GCC Code Coverage Report


Directory: ./
File: Python/compile.c
Date: 2022-06-27 09:02:04
Exec Total Coverage
Lines: 3747 4974 75.3%
Functions: 217 237 91.6%
Branches: 2698 4517 59.7%

Line Branch Exec Source
1 /*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is _PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
8 * 2. Builds a symbol table. See symtable.c.
9 * 3. Generate code for basic blocks. See compiler_mod() in this file.
10 * 4. Assemble the basic blocks into final code. See assemble() in
11 * this file.
12 * 5. Optimize the byte code (peephole optimizations).
13 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
16 *
17 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
20 * structure takes care of releasing those. Use the arena to manage
21 * objects.
22 */
23
24 #include <stdbool.h>
25
26 // Need _PyOpcode_RelativeJump of pycore_opcode.h
27 #define NEED_OPCODE_TABLES
28
29 #include "Python.h"
30 #include "pycore_ast.h" // _PyAST_GetDocString()
31 #include "pycore_code.h" // _PyCode_New()
32 #include "pycore_compile.h" // _PyFuture_FromAST()
33 #include "pycore_long.h" // _PyLong_GetZero()
34 #include "pycore_opcode.h" // _PyOpcode_Caches
35 #include "pycore_pymem.h" // _PyMem_IsPtrFreed()
36 #include "pycore_symtable.h" // PySTEntryObject
37
38
39 #define DEFAULT_BLOCK_SIZE 16
40 #define DEFAULT_CODE_SIZE 128
41 #define DEFAULT_LNOTAB_SIZE 16
42 #define DEFAULT_CNOTAB_SIZE 32
43
44 #define COMP_GENEXP 0
45 #define COMP_LISTCOMP 1
46 #define COMP_SETCOMP 2
47 #define COMP_DICTCOMP 3
48
49 /* A soft limit for stack use, to avoid excessive
50 * memory use for large constants, etc.
51 *
52 * The value 30 is plucked out of thin air.
53 * Code that could use more stack than this is
54 * rare, so the exact value is unimportant.
55 */
56 #define STACK_USE_GUIDELINE 30
57
58 /* If we exceed this limit, it should
59 * be considered a compiler bug.
60 * Currently it should be impossible
61 * to exceed STACK_USE_GUIDELINE * 100,
62 * as 100 is the maximum parse depth.
63 * For performance reasons we will
64 * want to reduce this to a
65 * few hundred in the future.
66 *
67 * NOTE: Whatever MAX_ALLOWED_STACK_USE is
68 * set to, it should never restrict what Python
69 * we can write, just how we compile it.
70 */
71 #define MAX_ALLOWED_STACK_USE (STACK_USE_GUIDELINE * 100)
72
73
74 /* Pseudo-instructions used in the compiler,
75 * but turned into NOPs or other instructions
76 * by the assembler. */
77 #define SETUP_FINALLY -1
78 #define SETUP_CLEANUP -2
79 #define SETUP_WITH -3
80 #define POP_BLOCK -4
81 #define JUMP -5
82 #define JUMP_NO_INTERRUPT -6
83 #define POP_JUMP_IF_FALSE -7
84 #define POP_JUMP_IF_TRUE -8
85 #define POP_JUMP_IF_NONE -9
86 #define POP_JUMP_IF_NOT_NONE -10
87 #define LOAD_METHOD -11
88
89 #define MIN_VIRTUAL_OPCODE -11
90 #define MAX_ALLOWED_OPCODE 254
91
92 #define IS_WITHIN_OPCODE_RANGE(opcode) \
93 ((opcode) >= MIN_VIRTUAL_OPCODE && (opcode) <= MAX_ALLOWED_OPCODE)
94
95 #define IS_VIRTUAL_OPCODE(opcode) ((opcode) < 0)
96
97 #define IS_VIRTUAL_JUMP_OPCODE(opcode) \
98 ((opcode) == JUMP || \
99 (opcode) == JUMP_NO_INTERRUPT || \
100 (opcode) == POP_JUMP_IF_NONE || \
101 (opcode) == POP_JUMP_IF_NOT_NONE || \
102 (opcode) == POP_JUMP_IF_FALSE || \
103 (opcode) == POP_JUMP_IF_TRUE)
104
105 #define IS_JUMP_OPCODE(opcode) \
106 (IS_VIRTUAL_JUMP_OPCODE(opcode) || \
107 is_bit_set_in_table(_PyOpcode_Jump, opcode))
108
109 #define IS_BLOCK_PUSH_OPCODE(opcode) \
110 ((opcode) == SETUP_FINALLY || \
111 (opcode) == SETUP_WITH || \
112 (opcode) == SETUP_CLEANUP)
113
114 /* opcodes which are not emitted in codegen stage, only by the assembler */
115 #define IS_ASSEMBLER_OPCODE(opcode) \
116 ((opcode) == JUMP_FORWARD || \
117 (opcode) == JUMP_BACKWARD || \
118 (opcode) == JUMP_BACKWARD_NO_INTERRUPT || \
119 (opcode) == POP_JUMP_FORWARD_IF_NONE || \
120 (opcode) == POP_JUMP_BACKWARD_IF_NONE || \
121 (opcode) == POP_JUMP_FORWARD_IF_NOT_NONE || \
122 (opcode) == POP_JUMP_BACKWARD_IF_NOT_NONE || \
123 (opcode) == POP_JUMP_FORWARD_IF_TRUE || \
124 (opcode) == POP_JUMP_BACKWARD_IF_TRUE || \
125 (opcode) == POP_JUMP_FORWARD_IF_FALSE || \
126 (opcode) == POP_JUMP_BACKWARD_IF_FALSE)
127
128
129 #define IS_BACKWARDS_JUMP_OPCODE(opcode) \
130 ((opcode) == JUMP_BACKWARD || \
131 (opcode) == JUMP_BACKWARD_NO_INTERRUPT || \
132 (opcode) == POP_JUMP_BACKWARD_IF_NONE || \
133 (opcode) == POP_JUMP_BACKWARD_IF_NOT_NONE || \
134 (opcode) == POP_JUMP_BACKWARD_IF_TRUE || \
135 (opcode) == POP_JUMP_BACKWARD_IF_FALSE)
136
137 #define IS_UNCONDITIONAL_JUMP_OPCODE(opcode) \
138 ((opcode) == JUMP || \
139 (opcode) == JUMP_NO_INTERRUPT || \
140 (opcode) == JUMP_FORWARD || \
141 (opcode) == JUMP_BACKWARD || \
142 (opcode) == JUMP_BACKWARD_NO_INTERRUPT)
143
144 #define IS_SCOPE_EXIT_OPCODE(opcode) \
145 ((opcode) == RETURN_VALUE || \
146 (opcode) == RAISE_VARARGS || \
147 (opcode) == RERAISE)
148
149 #define IS_TOP_LEVEL_AWAIT(c) ( \
150 (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
151 && (c->u->u_ste->ste_type == ModuleBlock))
152
153 struct location {
154 int lineno;
155 int end_lineno;
156 int col_offset;
157 int end_col_offset;
158 };
159
160 #define LOCATION(LNO, END_LNO, COL, END_COL) \
161 ((const struct location){(LNO), (END_LNO), (COL), (END_COL)})
162
163 static struct location NO_LOCATION = {-1, -1, -1, -1};
164
165 struct instr {
166 int i_opcode;
167 int i_oparg;
168 /* target block (if jump instruction) */
169 struct basicblock_ *i_target;
170 /* target block when exception is raised, should not be set by front-end. */
171 struct basicblock_ *i_except;
172 struct location i_loc;
173 };
174
175 typedef struct exceptstack {
176 struct basicblock_ *handlers[CO_MAXBLOCKS+1];
177 int depth;
178 } ExceptStack;
179
180 #define LOG_BITS_PER_INT 5
181 #define MASK_LOW_LOG_BITS 31
182
183 static inline int
184 134100507 is_bit_set_in_table(const uint32_t *table, int bitindex) {
185 /* Is the relevant bit set in the relevant word? */
186 /* 256 bits fit into 8 32-bits words.
187 * Word is indexed by (bitindex>>ln(size of int in bits)).
188 * Bit within word is the low bits of bitindex.
189 */
190
3/4
✓ Branch 0 taken 133624043 times.
✓ Branch 1 taken 476464 times.
✓ Branch 2 taken 133624043 times.
✗ Branch 3 not taken.
134100507 if (bitindex >= 0 && bitindex < 256) {
191 133624043 uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
192 133624043 return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
193 }
194 else {
195 476464 return 0;
196 }
197 }
198
199 static inline int
200 508695 is_relative_jump(struct instr *i)
201 {
202 508695 return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode);
203 }
204
205 static inline int
206 88285653 is_block_push(struct instr *i)
207 {
208
6/6
✓ Branch 0 taken 88192107 times.
✓ Branch 1 taken 93546 times.
✓ Branch 2 taken 88161579 times.
✓ Branch 3 taken 30528 times.
✓ Branch 4 taken 138263 times.
✓ Branch 5 taken 88023316 times.
88285653 return IS_BLOCK_PUSH_OPCODE(i->i_opcode);
209 }
210
211 static inline int
212 121994942 is_jump(struct instr *i)
213 {
214
14/14
✓ Branch 0 taken 121096541 times.
✓ Branch 1 taken 898401 times.
✓ Branch 2 taken 121081481 times.
✓ Branch 3 taken 15060 times.
✓ Branch 4 taken 120974501 times.
✓ Branch 5 taken 106980 times.
✓ Branch 6 taken 120895618 times.
✓ Branch 7 taken 78883 times.
✓ Branch 8 taken 119393103 times.
✓ Branch 9 taken 1502515 times.
✓ Branch 10 taken 118332792 times.
✓ Branch 11 taken 1060311 times.
✓ Branch 13 taken 2263369 times.
✓ Branch 14 taken 116069423 times.
121994942 return IS_JUMP_OPCODE(i->i_opcode);
215 }
216
217 static int
218 78780079 instr_size(struct instr *instruction)
219 {
220 78780079 int opcode = instruction->i_opcode;
221 assert(!IS_VIRTUAL_OPCODE(opcode));
222
2/2
✓ Branch 0 taken 71661025 times.
✓ Branch 1 taken 7119054 times.
78780079 int oparg = HAS_ARG(opcode) ? instruction->i_oparg : 0;
223 78780079 int extended_args = (0xFFFFFF < oparg) + (0xFFFF < oparg) + (0xFF < oparg);
224 78780079 int caches = _PyOpcode_Caches[opcode];
225 78780079 return extended_args + 1 + caches;
226 }
227
228 static void
229 14497150 write_instr(_Py_CODEUNIT *codestr, struct instr *instruction, int ilen)
230 {
231 14497150 int opcode = instruction->i_opcode;
232 assert(!IS_VIRTUAL_OPCODE(opcode));
233
2/2
✓ Branch 0 taken 13160338 times.
✓ Branch 1 taken 1336812 times.
14497150 int oparg = HAS_ARG(opcode) ? instruction->i_oparg : 0;
234 14497150 int caches = _PyOpcode_Caches[opcode];
235
2/5
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 332707 times.
✓ Branch 3 taken 14164443 times.
✗ Branch 4 not taken.
14497150 switch (ilen - caches) {
236 case 4:
237 *codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG_QUICK, (oparg >> 24) & 0xFF);
238 /* fall through */
239 case 3:
240 *codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG_QUICK, (oparg >> 16) & 0xFF);
241 /* fall through */
242 332707 case 2:
243 332707 *codestr++ = _Py_MAKECODEUNIT(EXTENDED_ARG_QUICK, (oparg >> 8) & 0xFF);
244 /* fall through */
245 14497150 case 1:
246 14497150 *codestr++ = _Py_MAKECODEUNIT(opcode, oparg & 0xFF);
247 14497150 break;
248 default:
249 Py_UNREACHABLE();
250 }
251
2/2
✓ Branch 0 taken 23124517 times.
✓ Branch 1 taken 14497150 times.
37621667 while (caches--) {
252 23124517 *codestr++ = _Py_MAKECODEUNIT(CACHE, 0);
253 }
254 14497150 }
255
256 typedef struct basicblock_ {
257 /* Each basicblock in a compilation unit is linked via b_list in the
258 reverse order that the block are allocated. b_list points to the next
259 block, not to be confused with b_next, which is next by control flow. */
260 struct basicblock_ *b_list;
261 /* Exception stack at start of block, used by assembler to create the exception handling table */
262 ExceptStack *b_exceptstack;
263 /* pointer to an array of instructions, initially NULL */
264 struct instr *b_instr;
265 /* If b_next is non-NULL, it is a pointer to the next
266 block reached by normal control flow. */
267 struct basicblock_ *b_next;
268 /* number of instructions used */
269 int b_iused;
270 /* length of instruction array (b_instr) */
271 int b_ialloc;
272 /* Number of predecssors that a block has. */
273 int b_predecessors;
274 /* Number of predecssors that a block has as an exception handler. */
275 int b_except_predecessors;
276 /* depth of stack upon entry of block, computed by stackdepth() */
277 int b_startdepth;
278 /* instruction offset for block, computed by assemble_jump_offsets() */
279 int b_offset;
280 /* Basic block is an exception handler that preserves lasti */
281 unsigned b_preserve_lasti : 1;
282 /* Used by compiler passes to mark whether they have visited a basic block. */
283 unsigned b_visited : 1;
284 /* b_cold is true if this block is not perf critical (like an exception handler) */
285 unsigned b_cold : 1;
286 /* b_warm is used by the cold-detection algorithm to mark blocks which are definitely not cold */
287 unsigned b_warm : 1;
288 } basicblock;
289
290
291 static struct instr *
292 24201404 basicblock_last_instr(basicblock *b) {
293
2/2
✓ Branch 0 taken 24143499 times.
✓ Branch 1 taken 57905 times.
24201404 if (b->b_iused) {
294 24143499 return &b->b_instr[b->b_iused - 1];
295 }
296 57905 return NULL;
297 }
298
299 static inline int
300 448800 basicblock_returns(basicblock *b) {
301 448800 struct instr *last = basicblock_last_instr(b);
302
4/4
✓ Branch 0 taken 408012 times.
✓ Branch 1 taken 40788 times.
✓ Branch 2 taken 224990 times.
✓ Branch 3 taken 183022 times.
448800 return last && last->i_opcode == RETURN_VALUE;
303 }
304
305 static inline int
306 1200809 basicblock_exits_scope(basicblock *b) {
307 1200809 struct instr *last = basicblock_last_instr(b);
308
8/8
✓ Branch 0 taken 1200785 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 960168 times.
✓ Branch 3 taken 240617 times.
✓ Branch 4 taken 856838 times.
✓ Branch 5 taken 103330 times.
✓ Branch 6 taken 16260 times.
✓ Branch 7 taken 840578 times.
1200809 return last && IS_SCOPE_EXIT_OPCODE(last->i_opcode);
309 }
310
311 static inline int
312 7872445 basicblock_nofallthrough(basicblock *b) {
313 7872445 struct instr *last = basicblock_last_instr(b);
314
2/2
✓ Branch 0 taken 7855352 times.
✓ Branch 1 taken 17093 times.
15727797 return (last &&
315
6/6
✓ Branch 0 taken 6017022 times.
✓ Branch 1 taken 1838330 times.
✓ Branch 2 taken 5350055 times.
✓ Branch 3 taken 666967 times.
✓ Branch 4 taken 5054794 times.
✓ Branch 5 taken 295261 times.
7855352 (IS_SCOPE_EXIT_OPCODE(last->i_opcode) ||
316
10/10
✓ Branch 0 taken 4541833 times.
✓ Branch 1 taken 512961 times.
✓ Branch 2 taken 4533494 times.
✓ Branch 3 taken 8339 times.
✓ Branch 4 taken 4444669 times.
✓ Branch 5 taken 88825 times.
✓ Branch 6 taken 4316357 times.
✓ Branch 7 taken 128312 times.
✓ Branch 8 taken 1588 times.
✓ Branch 9 taken 4314769 times.
5054794 IS_UNCONDITIONAL_JUMP_OPCODE(last->i_opcode)));
317 }
318
319 #define BB_NO_FALLTHROUGH(B) (basicblock_nofallthrough(B))
320 #define BB_HAS_FALLTHROUGH(B) (!basicblock_nofallthrough(B))
321
322 /* fblockinfo tracks the current frame block.
323
324 A frame block is used to handle loops, try/except, and try/finally.
325 It's called a frame block to distinguish it from a basic block in the
326 compiler IR.
327 */
328
329 enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
330 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER,
331 EXCEPTION_GROUP_HANDLER, ASYNC_COMPREHENSION_GENERATOR };
332
333 struct fblockinfo {
334 enum fblocktype fb_type;
335 basicblock *fb_block;
336 /* (optional) type-specific exit or cleanup block */
337 basicblock *fb_exit;
338 /* (optional) additional information required for unwinding */
339 void *fb_datum;
340 };
341
342 enum {
343 COMPILER_SCOPE_MODULE,
344 COMPILER_SCOPE_CLASS,
345 COMPILER_SCOPE_FUNCTION,
346 COMPILER_SCOPE_ASYNC_FUNCTION,
347 COMPILER_SCOPE_LAMBDA,
348 COMPILER_SCOPE_COMPREHENSION,
349 };
350
351 /* The following items change on entry and exit of code blocks.
352 They must be saved and restored when returning to a block.
353 */
354 struct compiler_unit {
355 PySTEntryObject *u_ste;
356
357 PyObject *u_name;
358 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
359 int u_scope_type;
360
361 /* The following fields are dicts that map objects to
362 the index of them in co_XXX. The index is used as
363 the argument for opcodes that refer to those collections.
364 */
365 PyObject *u_consts; /* all constants */
366 PyObject *u_names; /* all names */
367 PyObject *u_varnames; /* local variables */
368 PyObject *u_cellvars; /* cell variables */
369 PyObject *u_freevars; /* free variables */
370
371 PyObject *u_private; /* for private name mangling */
372
373 Py_ssize_t u_argcount; /* number of arguments for block */
374 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
375 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
376 /* Pointer to the most recently allocated block. By following b_list
377 members, you can reach all early allocated blocks. */
378 basicblock *u_blocks;
379 basicblock *u_curblock; /* pointer to current block */
380
381 int u_nfblocks;
382 struct fblockinfo u_fblock[CO_MAXBLOCKS];
383
384 int u_firstlineno; /* the first lineno of the block */
385 struct location u_loc; /* line/column info of the current stmt */
386 };
387
388 /* This struct captures the global state of a compilation.
389
390 The u pointer points to the current compilation unit, while units
391 for enclosing blocks are stored in c_stack. The u and c_stack are
392 managed by compiler_enter_scope() and compiler_exit_scope().
393
394 Note that we don't track recursion levels during compilation - the
395 task of detecting and rejecting excessive levels of nesting is
396 handled by the symbol analysis pass.
397
398 */
399
400 struct compiler {
401 PyObject *c_filename;
402 struct symtable *c_st;
403 PyFutureFeatures *c_future; /* pointer to module's __future__ */
404 PyCompilerFlags *c_flags;
405
406 int c_optimize; /* optimization level */
407 int c_interactive; /* true if in interactive mode */
408 int c_nestlevel;
409 PyObject *c_const_cache; /* Python dict holding all constants,
410 including names tuple */
411 struct compiler_unit *u; /* compiler state for current block */
412 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
413 PyArena *c_arena; /* pointer to memory allocation arena */
414 };
415
416 typedef struct {
417 // A list of strings corresponding to name captures. It is used to track:
418 // - Repeated name assignments in the same pattern.
419 // - Different name assignments in alternatives.
420 // - The order of name assignments in alternatives.
421 PyObject *stores;
422 // If 0, any name captures against our subject will raise.
423 int allow_irrefutable;
424 // An array of blocks to jump to on failure. Jumping to fail_pop[i] will pop
425 // i items off of the stack. The end result looks like this (with each block
426 // falling through to the next):
427 // fail_pop[4]: POP_TOP
428 // fail_pop[3]: POP_TOP
429 // fail_pop[2]: POP_TOP
430 // fail_pop[1]: POP_TOP
431 // fail_pop[0]: NOP
432 basicblock **fail_pop;
433 // The current length of fail_pop.
434 Py_ssize_t fail_pop_size;
435 // The number of items on top of the stack that need to *stay* on top of the
436 // stack. Variable captures go beneath these. All of them will be popped on
437 // failure.
438 Py_ssize_t on_top;
439 } pattern_context;
440
441 static int basicblock_next_instr(basicblock *);
442
443 static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
444 static void compiler_free(struct compiler *);
445 static basicblock *compiler_new_block(struct compiler *);
446 static int compiler_addop(struct compiler *, int, bool);
447 static int compiler_addop_i(struct compiler *, int, Py_ssize_t, bool);
448 static int compiler_addop_j(struct compiler *, int, basicblock *, bool);
449 static int compiler_error(struct compiler *, const char *, ...);
450 static int compiler_warn(struct compiler *, const char *, ...);
451 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
452
453 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
454 static int compiler_visit_stmt(struct compiler *, stmt_ty);
455 static int compiler_visit_keyword(struct compiler *, keyword_ty);
456 static int compiler_visit_expr(struct compiler *, expr_ty);
457 static int compiler_augassign(struct compiler *, stmt_ty);
458 static int compiler_annassign(struct compiler *, stmt_ty);
459 static int compiler_subscript(struct compiler *, expr_ty);
460 static int compiler_slice(struct compiler *, expr_ty);
461
462 static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
463
464
465 static int compiler_with(struct compiler *, stmt_ty, int);
466 static int compiler_async_with(struct compiler *, stmt_ty, int);
467 static int compiler_async_for(struct compiler *, stmt_ty);
468 static int validate_keywords(struct compiler *c, asdl_keyword_seq *keywords);
469 static int compiler_call_simple_kw_helper(struct compiler *c,
470 asdl_keyword_seq *keywords,
471 Py_ssize_t nkwelts);
472 static int compiler_call_helper(struct compiler *c, int n,
473 asdl_expr_seq *args,
474 asdl_keyword_seq *keywords);
475 static int compiler_try_except(struct compiler *, stmt_ty);
476 static int compiler_try_star_except(struct compiler *, stmt_ty);
477 static int compiler_set_qualname(struct compiler *);
478
479 static int compiler_sync_comprehension_generator(
480 struct compiler *c,
481 asdl_comprehension_seq *generators, int gen_index,
482 int depth,
483 expr_ty elt, expr_ty val, int type);
484
485 static int compiler_async_comprehension_generator(
486 struct compiler *c,
487 asdl_comprehension_seq *generators, int gen_index,
488 int depth,
489 expr_ty elt, expr_ty val, int type);
490
491 static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *);
492 static int compiler_match(struct compiler *, stmt_ty);
493 static int compiler_pattern_subpattern(struct compiler *, pattern_ty,
494 pattern_context *);
495
496 static void clean_basic_block(basicblock *bb);
497
498 static PyCodeObject *assemble(struct compiler *, int addNone);
499
500 #define CAPSULE_NAME "compile.c compiler unit"
501
502 PyObject *
503 12540737 _Py_Mangle(PyObject *privateobj, PyObject *ident)
504 {
505 /* Name mangling: __private becomes _classname__private.
506 This is independent from how the name is used. */
507 PyObject *result;
508 size_t nlen, plen, ipriv;
509 Py_UCS4 maxchar;
510
5/6
✓ Branch 0 taken 3129159 times.
✓ Branch 1 taken 9411578 times.
✓ Branch 4 taken 3129159 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 388044 times.
✓ Branch 7 taken 2741115 times.
15669896 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
511
2/2
✓ Branch 1 taken 255885 times.
✓ Branch 2 taken 132159 times.
3517203 PyUnicode_READ_CHAR(ident, 0) != '_' ||
512 388044 PyUnicode_READ_CHAR(ident, 1) != '_') {
513 12408578 Py_INCREF(ident);
514 12408578 return ident;
515 }
516 132159 nlen = PyUnicode_GET_LENGTH(ident);
517 132159 plen = PyUnicode_GET_LENGTH(privateobj);
518 /* Don't mangle __id__ or names with dots.
519
520 The only time a name with a dot can occur is when
521 we are compiling an import statement that has a
522 package name.
523
524 TODO(jhylton): Decide whether we want to support
525 mangling of the module name, e.g. __M.X.
526 */
527
4/4
✓ Branch 1 taken 130324 times.
✓ Branch 2 taken 1835 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 130321 times.
262483 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
528
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1838 times.
132162 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
529 1838 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
530 130321 Py_INCREF(ident);
531 130321 return ident; /* Don't mangle __whatever__ */
532 }
533 /* Strip leading underscores from class name */
534 1838 ipriv = 0;
535
2/2
✓ Branch 1 taken 491 times.
✓ Branch 2 taken 1838 times.
2329 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
536 491 ipriv++;
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1838 times.
1838 if (ipriv == plen) {
538 Py_INCREF(ident);
539 return ident; /* Don't mangle if class is just underscores */
540 }
541 1838 plen -= ipriv;
542
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1838 times.
1838 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
544 PyErr_SetString(PyExc_OverflowError,
545 "private identifier too large to be mangled");
546 return NULL;
547 }
548
549 1838 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
550
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1838 times.
1838 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
551 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
552
553 1838 result = PyUnicode_New(1 + nlen + plen, maxchar);
554
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1838 times.
1838 if (!result)
555 return 0;
556 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
557 1838 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
558
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1838 times.
1838 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
559 Py_DECREF(result);
560 return NULL;
561 }
562
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1838 times.
1838 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
563 Py_DECREF(result);
564 return NULL;
565 }
566 assert(_PyUnicode_CheckConsistency(result, 1));
567 1838 return result;
568 }
569
570 static int
571 156685 compiler_init(struct compiler *c)
572 {
573 156685 memset(c, 0, sizeof(struct compiler));
574
575 156685 c->c_const_cache = PyDict_New();
576
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156685 times.
156685 if (!c->c_const_cache) {
577 return 0;
578 }
579
580 156685 c->c_stack = PyList_New(0);
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156685 times.
156685 if (!c->c_stack) {
582 Py_CLEAR(c->c_const_cache);
583 return 0;
584 }
585
586 156685 return 1;
587 }
588
589 PyCodeObject *
590 156685 _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
591 int optimize, PyArena *arena)
592 {
593 struct compiler c;
594 156685 PyCodeObject *co = NULL;
595 156685 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
596 int merged;
597
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 156685 times.
156685 if (!compiler_init(&c))
598 return NULL;
599 156685 Py_INCREF(filename);
600 156685 c.c_filename = filename;
601 156685 c.c_arena = arena;
602 156685 c.c_future = _PyFuture_FromAST(mod, filename);
603
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156685 times.
156685 if (c.c_future == NULL)
604 goto finally;
605
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 156681 times.
156685 if (!flags) {
606 4 flags = &local_flags;
607 }
608 156685 merged = c.c_future->ff_features | flags->cf_flags;
609 156685 c.c_future->ff_features = merged;
610 156685 flags->cf_flags = merged;
611 156685 c.c_flags = flags;
612
2/2
✓ Branch 0 taken 156661 times.
✓ Branch 1 taken 24 times.
156685 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
613 156685 c.c_nestlevel = 0;
614
615 _PyASTOptimizeState state;
616 156685 state.optimize = c.c_optimize;
617 156685 state.ff_features = merged;
618
619
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 156685 times.
156685 if (!_PyAST_Optimize(mod, arena, &state)) {
620 goto finally;
621 }
622
623 156685 c.c_st = _PySymtable_Build(mod, filename, c.c_future);
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156685 times.
156685 if (c.c_st == NULL) {
625 if (!PyErr_Occurred())
626 PyErr_SetString(PyExc_SystemError, "no symtable");
627 goto finally;
628 }
629
630 156685 co = compiler_mod(&c, mod);
631
632 156685 finally:
633 156685 compiler_free(&c);
634 assert(co || PyErr_Occurred());
635 156685 return co;
636 }
637
638 static void
639 156685 compiler_free(struct compiler *c)
640 {
641
1/2
✓ Branch 0 taken 156685 times.
✗ Branch 1 not taken.
156685 if (c->c_st)
642 156685 _PySymtable_Free(c->c_st);
643
1/2
✓ Branch 0 taken 156685 times.
✗ Branch 1 not taken.
156685 if (c->c_future)
644 156685 PyObject_Free(c->c_future);
645 156685 Py_XDECREF(c->c_filename);
646 156685 Py_DECREF(c->c_const_cache);
647 156685 Py_DECREF(c->c_stack);
648 156685 }
649
650 static PyObject *
651 448800 list2dict(PyObject *list)
652 {
653 Py_ssize_t i, n;
654 PyObject *v, *k;
655 448800 PyObject *dict = PyDict_New();
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (!dict) return NULL;
657
658 448800 n = PyList_Size(list);
659
2/2
✓ Branch 0 taken 973930 times.
✓ Branch 1 taken 448800 times.
1422730 for (i = 0; i < n; i++) {
660 973930 v = PyLong_FromSsize_t(i);
661
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 973930 times.
973930 if (!v) {
662 Py_DECREF(dict);
663 return NULL;
664 }
665 973930 k = PyList_GET_ITEM(list, i);
666
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 973930 times.
973930 if (PyDict_SetItem(dict, k, v) < 0) {
667 Py_DECREF(v);
668 Py_DECREF(dict);
669 return NULL;
670 }
671 973930 Py_DECREF(v);
672 }
673 448800 return dict;
674 }
675
676 /* Return new dict containing names from src that match scope(s).
677
678 src is a symbol table dictionary. If the scope of a name matches
679 either scope_type or flag is set, insert it into the new dict. The
680 values are integers, starting at offset and increasing by one for
681 each key.
682 */
683
684 static PyObject *
685 897600 dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
686 {
687 897600 Py_ssize_t i = offset, scope, num_keys, key_i;
688 897600 PyObject *k, *v, *dest = PyDict_New();
689 PyObject *sorted_keys;
690
691 assert(offset >= 0);
692
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 897600 times.
897600 if (dest == NULL)
693 return NULL;
694
695 /* Sort the keys so that we have a deterministic order on the indexes
696 saved in the returned dictionary. These indexes are used as indexes
697 into the free and cell var storage. Therefore if they aren't
698 deterministic, then the generated bytecode is not deterministic.
699 */
700 897600 sorted_keys = PyDict_Keys(src);
701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 897600 times.
897600 if (sorted_keys == NULL)
702 return NULL;
703
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 897600 times.
897600 if (PyList_Sort(sorted_keys) != 0) {
704 Py_DECREF(sorted_keys);
705 return NULL;
706 }
707 897600 num_keys = PyList_GET_SIZE(sorted_keys);
708
709
2/2
✓ Branch 0 taken 4296772 times.
✓ Branch 1 taken 897600 times.
5194372 for (key_i = 0; key_i < num_keys; key_i++) {
710 /* XXX this should probably be a macro in symtable.h */
711 long vi;
712 4296772 k = PyList_GET_ITEM(sorted_keys, key_i);
713 4296772 v = PyDict_GetItemWithError(src, k);
714 assert(v && PyLong_Check(v));
715 4296772 vi = PyLong_AS_LONG(v);
716 4296772 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
717
718
4/4
✓ Branch 0 taken 4251511 times.
✓ Branch 1 taken 45261 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 4251505 times.
4296772 if (scope == scope_type || vi & flag) {
719 45267 PyObject *item = PyLong_FromSsize_t(i);
720
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 45267 times.
45267 if (item == NULL) {
721 Py_DECREF(sorted_keys);
722 Py_DECREF(dest);
723 return NULL;
724 }
725 45267 i++;
726
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 45267 times.
45267 if (PyDict_SetItem(dest, k, item) < 0) {
727 Py_DECREF(sorted_keys);
728 Py_DECREF(item);
729 Py_DECREF(dest);
730 return NULL;
731 }
732 45267 Py_DECREF(item);
733 }
734 }
735 897600 Py_DECREF(sorted_keys);
736 897600 return dest;
737 }
738
739 static void
740 740915 compiler_unit_check(struct compiler_unit *u)
741 {
742 basicblock *block;
743
2/2
✓ Branch 0 taken 2153882 times.
✓ Branch 1 taken 740915 times.
2894797 for (block = u->u_blocks; block != NULL; block = block->b_list) {
744 assert(!_PyMem_IsPtrFreed(block));
745 2153882 if (block->b_instr != NULL) {
746 assert(block->b_ialloc > 0);
747 assert(block->b_iused >= 0);
748 assert(block->b_ialloc >= block->b_iused);
749 }
750 else {
751 assert (block->b_iused == 0);
752 assert (block->b_ialloc == 0);
753 }
754 }
755 740915 }
756
757 static void
758 448800 compiler_unit_free(struct compiler_unit *u)
759 {
760 basicblock *b, *next;
761
762 448800 compiler_unit_check(u);
763 448800 b = u->u_blocks;
764
2/2
✓ Branch 0 taken 1380907 times.
✓ Branch 1 taken 448800 times.
1829707 while (b != NULL) {
765
2/2
✓ Branch 0 taken 1308374 times.
✓ Branch 1 taken 72533 times.
1380907 if (b->b_instr)
766 1308374 PyObject_Free((void *)b->b_instr);
767 1380907 next = b->b_list;
768 1380907 PyObject_Free((void *)b);
769 1380907 b = next;
770 }
771
1/2
✓ Branch 0 taken 448800 times.
✗ Branch 1 not taken.
448800 Py_CLEAR(u->u_ste);
772
1/2
✓ Branch 0 taken 448800 times.
✗ Branch 1 not taken.
448800 Py_CLEAR(u->u_name);
773
2/2
✓ Branch 0 taken 292115 times.
✓ Branch 1 taken 156685 times.
448800 Py_CLEAR(u->u_qualname);
774
1/2
✓ Branch 0 taken 448800 times.
✗ Branch 1 not taken.
448800 Py_CLEAR(u->u_consts);
775
1/2
✓ Branch 0 taken 448800 times.
✗ Branch 1 not taken.
448800 Py_CLEAR(u->u_names);
776
1/2
✓ Branch 0 taken 448800 times.
✗ Branch 1 not taken.
448800 Py_CLEAR(u->u_varnames);
777
1/2
✓ Branch 0 taken 448800 times.
✗ Branch 1 not taken.
448800 Py_CLEAR(u->u_freevars);
778
1/2
✓ Branch 0 taken 448800 times.
✗ Branch 1 not taken.
448800 Py_CLEAR(u->u_cellvars);
779
2/2
✓ Branch 0 taken 99263 times.
✓ Branch 1 taken 349537 times.
448800 Py_CLEAR(u->u_private);
780 448800 PyObject_Free(u);
781 448800 }
782
783 static int
784 292115 compiler_set_qualname(struct compiler *c)
785 {
786 Py_ssize_t stack_size;
787 292115 struct compiler_unit *u = c->u;
788 PyObject *name, *base;
789
790 292115 base = NULL;
791 292115 stack_size = PyList_GET_SIZE(c->c_stack);
792 assert(stack_size >= 1);
793
2/2
✓ Branch 0 taken 108434 times.
✓ Branch 1 taken 183681 times.
292115 if (stack_size > 1) {
794 108434 int scope, force_global = 0;
795 struct compiler_unit *parent;
796 PyObject *mangled, *capsule;
797
798 108434 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
799 108434 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
800 assert(parent);
801
802
2/2
✓ Branch 0 taken 23723 times.
✓ Branch 1 taken 84711 times.
108434 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
803
2/2
✓ Branch 0 taken 22662 times.
✓ Branch 1 taken 1061 times.
23723 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
804
2/2
✓ Branch 0 taken 947 times.
✓ Branch 1 taken 21715 times.
22662 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
805 assert(u->u_name);
806 86719 mangled = _Py_Mangle(parent->u_private, u->u_name);
807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86719 times.
86719 if (!mangled)
808 return 0;
809 86719 scope = _PyST_GetScope(parent->u_ste, mangled);
810 86719 Py_DECREF(mangled);
811 assert(scope != GLOBAL_IMPLICIT);
812
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 86718 times.
86719 if (scope == GLOBAL_EXPLICIT)
813 1 force_global = 1;
814 }
815
816
2/2
✓ Branch 0 taken 108433 times.
✓ Branch 1 taken 1 times.
108434 if (!force_global) {
817
2/2
✓ Branch 0 taken 73879 times.
✓ Branch 1 taken 34554 times.
108433 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
818
2/2
✓ Branch 0 taken 73756 times.
✓ Branch 1 taken 123 times.
73879 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
819
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 73633 times.
73756 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA)
820 {
821 _Py_DECLARE_STR(dot_locals, ".<locals>");
822 34800 base = PyUnicode_Concat(parent->u_qualname,
823 &_Py_STR(dot_locals));
824
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 34800 times.
34800 if (base == NULL)
825 return 0;
826 }
827 else {
828 73633 Py_INCREF(parent->u_qualname);
829 73633 base = parent->u_qualname;
830 }
831 }
832 }
833
834
2/2
✓ Branch 0 taken 108433 times.
✓ Branch 1 taken 183682 times.
292115 if (base != NULL) {
835 _Py_DECLARE_STR(dot, ".");
836 108433 name = PyUnicode_Concat(base, &_Py_STR(dot));
837 108433 Py_DECREF(base);
838
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108433 times.
108433 if (name == NULL)
839 return 0;
840 108433 PyUnicode_Append(&name, u->u_name);
841
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108433 times.
108433 if (name == NULL)
842 return 0;
843 }
844 else {
845 183682 Py_INCREF(u->u_name);
846 183682 name = u->u_name;
847 }
848 292115 u->u_qualname = name;
849
850 292115 return 1;
851 }
852
853
854 /* Allocate a new block and return a pointer to it.
855 Returns NULL on error.
856 */
857 static basicblock *
858 1380907 new_basicblock()
859 {
860 1380907 basicblock *b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
861
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1380907 times.
1380907 if (b == NULL) {
862 PyErr_NoMemory();
863 return NULL;
864 }
865 1380907 return b;
866 }
867
868 static basicblock *
869 1366225 compiler_new_block(struct compiler *c)
870 {
871 1366225 basicblock *b = new_basicblock();
872
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1366225 times.
1366225 if (b == NULL) {
873 return NULL;
874 }
875 /* Extend the singly linked list of blocks with new block. */
876 1366225 struct compiler_unit *u = c->u;
877 1366225 b->b_list = u->u_blocks;
878 1366225 u->u_blocks = b;
879 1366225 return b;
880 }
881
882 static basicblock *
883 917425 compiler_use_next_block(struct compiler *c, basicblock *block)
884 {
885 assert(block != NULL);
886 917425 c->u->u_curblock->b_next = block;
887 917425 c->u->u_curblock = block;
888 917425 return block;
889 }
890
891 static basicblock *
892 14682 basicblock_new_b_list_successor(basicblock *prev)
893 {
894 14682 basicblock *result = new_basicblock();
895
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14682 times.
14682 if (result == NULL) {
896 return NULL;
897 }
898 14682 result->b_list = prev->b_list;
899 14682 prev->b_list = result;
900 14682 return result;
901 }
902
903 static basicblock *
904 14660 copy_basicblock(basicblock *block)
905 {
906 /* Cannot copy a block if it has a fallthrough, since
907 * a block can only have one fallthrough predecessor.
908 */
909 assert(BB_NO_FALLTHROUGH(block));
910 14660 basicblock *result = basicblock_new_b_list_successor(block);
911
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14660 times.
14660 if (result == NULL) {
912 return NULL;
913 }
914
2/2
✓ Branch 0 taken 35278 times.
✓ Branch 1 taken 14660 times.
49938 for (int i = 0; i < block->b_iused; i++) {
915 35278 int n = basicblock_next_instr(result);
916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35278 times.
35278 if (n < 0) {
917 return NULL;
918 }
919 35278 result->b_instr[n] = block->b_instr[i];
920 }
921 14660 return result;
922 }
923
924 /* Returns the offset of the next instruction in the current block's
925 b_instr array. Resizes the b_instr as necessary.
926 Returns -1 on failure.
927 */
928
929 static int
930 15833640 basicblock_next_instr(basicblock *b)
931 {
932 assert(b != NULL);
933
2/2
✓ Branch 0 taken 1308374 times.
✓ Branch 1 taken 14525266 times.
15833640 if (b->b_instr == NULL) {
934 1308374 b->b_instr = (struct instr *)PyObject_Calloc(
935 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1308374 times.
1308374 if (b->b_instr == NULL) {
937 PyErr_NoMemory();
938 return -1;
939 }
940 1308374 b->b_ialloc = DEFAULT_BLOCK_SIZE;
941 }
942
2/2
✓ Branch 0 taken 262437 times.
✓ Branch 1 taken 14262829 times.
14525266 else if (b->b_iused == b->b_ialloc) {
943 struct instr *tmp;
944 size_t oldsize, newsize;
945 262437 oldsize = b->b_ialloc * sizeof(struct instr);
946 262437 newsize = oldsize << 1;
947
948
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262437 times.
262437 if (oldsize > (SIZE_MAX >> 1)) {
949 PyErr_NoMemory();
950 return -1;
951 }
952
953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262437 times.
262437 if (newsize == 0) {
954 PyErr_NoMemory();
955 return -1;
956 }
957 262437 b->b_ialloc <<= 1;
958 262437 tmp = (struct instr *)PyObject_Realloc(
959 262437 (void *)b->b_instr, newsize);
960
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 262437 times.
262437 if (tmp == NULL) {
961 PyErr_NoMemory();
962 return -1;
963 }
964 262437 b->b_instr = tmp;
965 262437 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
966 }
967 15833640 return b->b_iused++;
968 }
969
970 /* Set the line number and column offset for the following instructions.
971
972 The line number is reset in the following cases:
973 - when entering a new scope
974 - on each statement
975 - on each expression and sub-expression
976 - before the "except" and "finally" clauses
977 */
978
979 #define SET_LOC(c, x) \
980 (c)->u->u_loc.lineno = (x)->lineno; \
981 (c)->u->u_loc.end_lineno = (x)->end_lineno; \
982 (c)->u->u_loc.col_offset = (x)->col_offset; \
983 (c)->u->u_loc.end_col_offset = (x)->end_col_offset;
984
985 // Artificial instructions
986 #define UNSET_LOC(c) \
987 (c)->u->u_loc.lineno = -1; \
988 (c)->u->u_loc.end_lineno = -1; \
989 (c)->u->u_loc.col_offset = -1; \
990 (c)->u->u_loc.end_col_offset = -1;
991
992
993 /* Return the stack effect of opcode with argument oparg.
994
995 Some opcodes have different stack effect when jump to the target and
996 when not jump. The 'jump' parameter specifies the case:
997
998 * 0 -- when not jump
999 * 1 -- when jump
1000 * -1 -- maximal
1001 */
1002 static int
1003 15033227 stack_effect(int opcode, int oparg, int jump)
1004 {
1005
75/81
✓ Branch 0 taken 469054 times.
✓ Branch 1 taken 249989 times.
✓ Branch 2 taken 12511 times.
✓ Branch 3 taken 47921 times.
✓ Branch 4 taken 32637 times.
✓ Branch 5 taken 122676 times.
✓ Branch 6 taken 134253 times.
✓ Branch 7 taken 24281 times.
✓ Branch 8 taken 1167 times.
✓ Branch 9 taken 50361 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 15204 times.
✓ Branch 12 taken 514681 times.
✓ Branch 13 taken 1358 times.
✓ Branch 14 taken 487 times.
✓ Branch 15 taken 11915 times.
✓ Branch 16 taken 33655 times.
✓ Branch 17 taken 31999 times.
✓ Branch 18 taken 366037 times.
✓ Branch 19 taken 378 times.
✓ Branch 20 taken 36259 times.
✓ Branch 21 taken 37 times.
✓ Branch 22 taken 100734 times.
✓ Branch 23 taken 3316 times.
✓ Branch 24 taken 72050 times.
✓ Branch 25 taken 161 times.
✓ Branch 26 taken 972 times.
✓ Branch 27 taken 4 times.
✓ Branch 28 taken 2193257 times.
✓ Branch 29 taken 226484 times.
✓ Branch 30 taken 290457 times.
✓ Branch 31 taken 28632 times.
✓ Branch 32 taken 9699 times.
✓ Branch 33 taken 707499 times.
✓ Branch 34 taken 153260 times.
✓ Branch 35 taken 11683 times.
✗ Branch 36 not taken.
✓ Branch 37 taken 56055 times.
✓ Branch 38 taken 82392 times.
✓ Branch 39 taken 194860 times.
✓ Branch 40 taken 26856 times.
✓ Branch 41 taken 540296 times.
✓ Branch 42 taken 1750259 times.
✓ Branch 43 taken 26726 times.
✓ Branch 44 taken 39502 times.
✓ Branch 45 taken 8722 times.
✗ Branch 46 not taken.
✓ Branch 47 taken 37463 times.
✓ Branch 48 taken 17721 times.
✓ Branch 49 taken 4361 times.
✓ Branch 50 taken 2285391 times.
✓ Branch 51 taken 471886 times.
✓ Branch 52 taken 3359 times.
✓ Branch 53 taken 9384 times.
✓ Branch 54 taken 100017 times.
✓ Branch 55 taken 53716 times.
✓ Branch 56 taken 1411306 times.
✓ Branch 57 taken 17575 times.
✓ Branch 58 taken 292108 times.
✓ Branch 59 taken 11373 times.
✓ Branch 60 taken 38916 times.
✓ Branch 61 taken 30119 times.
✓ Branch 62 taken 75370 times.
✓ Branch 63 taken 11697 times.
✓ Branch 64 taken 4 times.
✓ Branch 65 taken 1109 times.
✓ Branch 66 taken 4361 times.
✓ Branch 67 taken 3 times.
✓ Branch 68 taken 3 times.
✓ Branch 69 taken 546 times.
✓ Branch 70 taken 3 times.
✓ Branch 71 taken 33779 times.
✗ Branch 72 not taken.
✓ Branch 73 taken 78672 times.
✓ Branch 74 taken 4919 times.
✓ Branch 75 taken 32829 times.
✓ Branch 76 taken 4 times.
✗ Branch 77 not taken.
✓ Branch 78 taken 140965 times.
✓ Branch 79 taken 1183532 times.
✗ Branch 80 not taken.
15033227 switch (opcode) {
1006 469054 case NOP:
1007 case EXTENDED_ARG:
1008 case RESUME:
1009 case CACHE:
1010 469054 return 0;
1011
1012 /* Stack manipulation */
1013 249989 case POP_TOP:
1014 249989 return -1;
1015 12511 case SWAP:
1016 12511 return 0;
1017
1018 /* Unary operators */
1019 47921 case UNARY_POSITIVE:
1020 case UNARY_NEGATIVE:
1021 case UNARY_NOT:
1022 case UNARY_INVERT:
1023 47921 return 0;
1024
1025 32637 case SET_ADD:
1026 case LIST_APPEND:
1027 32637 return -1;
1028 122676 case MAP_ADD:
1029 122676 return -2;
1030
1031 134253 case BINARY_SUBSCR:
1032 134253 return -1;
1033 24281 case STORE_SUBSCR:
1034 24281 return -3;
1035 1167 case DELETE_SUBSCR:
1036 1167 return -2;
1037
1038 50361 case GET_ITER:
1039 50361 return 0;
1040
1041 case PRINT_EXPR:
1042 return -1;
1043 15204 case LOAD_BUILD_CLASS:
1044 15204 return 1;
1045
1046 514681 case RETURN_VALUE:
1047 514681 return -1;
1048 1358 case IMPORT_STAR:
1049 1358 return -1;
1050 487 case SETUP_ANNOTATIONS:
1051 487 return 0;
1052 11915 case ASYNC_GEN_WRAP:
1053 case YIELD_VALUE:
1054 11915 return 0;
1055 33655 case POP_BLOCK:
1056 33655 return 0;
1057 31999 case POP_EXCEPT:
1058 31999 return -1;
1059
1060 366037 case STORE_NAME:
1061 366037 return -1;
1062 378 case DELETE_NAME:
1063 378 return 0;
1064 36259 case UNPACK_SEQUENCE:
1065 36259 return oparg-1;
1066 37 case UNPACK_EX:
1067 37 return (oparg&0xFF) + (oparg>>8);
1068 100734 case FOR_ITER:
1069 /* -1 at end of iterator, 1 if continue iterating. */
1070
2/2
✓ Branch 0 taken 50367 times.
✓ Branch 1 taken 50367 times.
100734 return jump > 0 ? -1 : 1;
1071 3316 case SEND:
1072
2/2
✓ Branch 0 taken 1658 times.
✓ Branch 1 taken 1658 times.
3316 return jump > 0 ? -1 : 0;
1073 72050 case STORE_ATTR:
1074 72050 return -2;
1075 161 case DELETE_ATTR:
1076 161 return -1;
1077 972 case STORE_GLOBAL:
1078 972 return -1;
1079 4 case DELETE_GLOBAL:
1080 4 return 0;
1081 2193257 case LOAD_CONST:
1082 2193257 return 1;
1083 226484 case LOAD_NAME:
1084 226484 return 1;
1085 290457 case BUILD_TUPLE:
1086 case BUILD_LIST:
1087 case BUILD_SET:
1088 case BUILD_STRING:
1089 290457 return 1-oparg;
1090 28632 case BUILD_MAP:
1091 28632 return 1 - 2*oparg;
1092 9699 case BUILD_CONST_KEY_MAP:
1093 9699 return -oparg;
1094 707499 case LOAD_ATTR:
1095 707499 return (oparg & 1);
1096 153260 case COMPARE_OP:
1097 case IS_OP:
1098 case CONTAINS_OP:
1099 153260 return -1;
1100 11683 case CHECK_EXC_MATCH:
1101 11683 return 0;
1102 case CHECK_EG_MATCH:
1103 return 0;
1104 56055 case IMPORT_NAME:
1105 56055 return -1;
1106 82392 case IMPORT_FROM:
1107 82392 return 1;
1108
1109 /* Jumps */
1110 194860 case JUMP_FORWARD:
1111 case JUMP_BACKWARD:
1112 case JUMP:
1113 case JUMP_BACKWARD_NO_INTERRUPT:
1114 case JUMP_NO_INTERRUPT:
1115 194860 return 0;
1116
1117 26856 case JUMP_IF_TRUE_OR_POP:
1118 case JUMP_IF_FALSE_OR_POP:
1119
2/2
✓ Branch 0 taken 13428 times.
✓ Branch 1 taken 13428 times.
26856 return jump ? 0 : -1;
1120
1121 540296 case POP_JUMP_BACKWARD_IF_NONE:
1122 case POP_JUMP_FORWARD_IF_NONE:
1123 case POP_JUMP_IF_NONE:
1124 case POP_JUMP_BACKWARD_IF_NOT_NONE:
1125 case POP_JUMP_FORWARD_IF_NOT_NONE:
1126 case POP_JUMP_IF_NOT_NONE:
1127 case POP_JUMP_FORWARD_IF_FALSE:
1128 case POP_JUMP_BACKWARD_IF_FALSE:
1129 case POP_JUMP_IF_FALSE:
1130 case POP_JUMP_FORWARD_IF_TRUE:
1131 case POP_JUMP_BACKWARD_IF_TRUE:
1132 case POP_JUMP_IF_TRUE:
1133 540296 return -1;
1134
1135 1750259 case LOAD_GLOBAL:
1136 1750259 return (oparg & 1) + 1;
1137
1138 /* Exception handling pseudo-instructions */
1139 26726 case SETUP_FINALLY:
1140 /* 0 in the normal flow.
1141 * Restore the stack position and push 1 value before jumping to
1142 * the handler if an exception be raised. */
1143 26726 return jump ? 1 : 0;
1144 39502 case SETUP_CLEANUP:
1145 /* As SETUP_FINALLY, but pushes lasti as well */
1146
2/2
✓ Branch 0 taken 19751 times.
✓ Branch 1 taken 19751 times.
39502 return jump ? 2 : 0;
1147 8722 case SETUP_WITH:
1148 /* 0 in the normal flow.
1149 * Restore the stack position to the position before the result
1150 * of __(a)enter__ and push 2 values before jumping to the handler
1151 * if an exception be raised. */
1152 8722 return jump ? 1 : 0;
1153
1154 case PREP_RERAISE_STAR:
1155 return -1;
1156 37463 case RERAISE:
1157 37463 return -1;
1158 17721 case PUSH_EXC_INFO:
1159 17721 return 1;
1160
1161 4361 case WITH_EXCEPT_START:
1162 4361 return 1;
1163
1164 2285391 case LOAD_FAST:
1165 case LOAD_FAST_CHECK:
1166 2285391 return 1;
1167 471886 case STORE_FAST:
1168 471886 return -1;
1169 3359 case DELETE_FAST:
1170 3359 return 0;
1171
1172 9384 case RETURN_GENERATOR:
1173 9384 return 0;
1174
1175 100017 case RAISE_VARARGS:
1176 100017 return -oparg;
1177
1178 /* Functions and calls */
1179 53716 case KW_NAMES:
1180 53716 return 0;
1181 1411306 case CALL:
1182 1411306 return -1-oparg;
1183
1184 17575 case CALL_FUNCTION_EX:
1185
2/2
✓ Branch 0 taken 10318 times.
✓ Branch 1 taken 7257 times.
17575 return -2 - ((oparg & 0x01) != 0);
1186 292108 case MAKE_FUNCTION:
1187 292108 return 0 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1188 292108 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
1189 11373 case BUILD_SLICE:
1190
2/2
✓ Branch 0 taken 455 times.
✓ Branch 1 taken 10918 times.
11373 if (oparg == 3)
1191 455 return -2;
1192 else
1193 10918 return -1;
1194
1195 /* Closures */
1196 38916 case MAKE_CELL:
1197 case COPY_FREE_VARS:
1198 38916 return 0;
1199 30119 case LOAD_CLOSURE:
1200 30119 return 1;
1201 75370 case LOAD_DEREF:
1202 case LOAD_CLASSDEREF:
1203 75370 return 1;
1204 11697 case STORE_DEREF:
1205 11697 return -1;
1206 4 case DELETE_DEREF:
1207 4 return 0;
1208
1209 /* Iterators and generators */
1210 1109 case GET_AWAITABLE:
1211 1109 return 0;
1212
1213 4361 case BEFORE_ASYNC_WITH:
1214 case BEFORE_WITH:
1215 4361 return 1;
1216 3 case GET_AITER:
1217 3 return 0;
1218 3 case GET_ANEXT:
1219 3 return 1;
1220 546 case GET_YIELD_FROM_ITER:
1221 546 return 0;
1222 3 case END_ASYNC_FOR:
1223 3 return -2;
1224 33779 case FORMAT_VALUE:
1225 /* If there's a fmt_spec on the stack, we go from 2->1,
1226 else 1->1. */
1227
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 33702 times.
33779 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
1228 case LOAD_METHOD:
1229 return 1;
1230 78672 case LOAD_ASSERTION_ERROR:
1231 78672 return 1;
1232 4919 case LIST_TO_TUPLE:
1233 4919 return 0;
1234 32829 case LIST_EXTEND:
1235 case SET_UPDATE:
1236 case DICT_MERGE:
1237 case DICT_UPDATE:
1238 32829 return -1;
1239 4 case MATCH_CLASS:
1240 4 return -2;
1241 case GET_LEN:
1242 case MATCH_MAPPING:
1243 case MATCH_SEQUENCE:
1244 case MATCH_KEYS:
1245 return 1;
1246 140965 case COPY:
1247 case PUSH_NULL:
1248 140965 return 1;
1249 1183532 case BINARY_OP:
1250 1183532 return -1;
1251 default:
1252 return PY_INVALID_STACK_EFFECT;
1253 }
1254 return PY_INVALID_STACK_EFFECT; /* not reachable */
1255 }
1256
1257 int
1258 PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1259 {
1260 return stack_effect(opcode, oparg, jump);
1261 }
1262
1263 int
1264 PyCompile_OpcodeStackEffect(int opcode, int oparg)
1265 {
1266 return stack_effect(opcode, oparg, -1);
1267 }
1268
1269 static int
1270 14679350 is_end_of_basic_block(struct instr *instr)
1271 {
1272 14679350 int opcode = instr->i_opcode;
1273
7/8
✓ Branch 1 taken 14404101 times.
✓ Branch 2 taken 275249 times.
✓ Branch 3 taken 14386470 times.
✓ Branch 4 taken 17631 times.
✓ Branch 5 taken 14380007 times.
✓ Branch 6 taken 6463 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14380007 times.
14679350 return is_jump(instr) || IS_SCOPE_EXIT_OPCODE(opcode);
1274 }
1275
1276 static int
1277 15673699 compiler_use_new_implicit_block_if_needed(struct compiler *c)
1278 {
1279 15673699 basicblock *b = c->u->u_curblock;
1280
4/4
✓ Branch 0 taken 14679350 times.
✓ Branch 1 taken 994349 times.
✓ Branch 4 taken 299343 times.
✓ Branch 5 taken 14380007 times.
15673699 if (b->b_iused && is_end_of_basic_block(basicblock_last_instr(b))) {
1281 299343 basicblock *b = compiler_new_block(c);
1282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 299343 times.
299343 if (b == NULL) {
1283 return -1;
1284 }
1285 299343 compiler_use_next_block(c, b);
1286 }
1287 15673699 return 0;
1288 }
1289
1290 /* Add an opcode with no argument.
1291 Returns 0 on failure, 1 on success.
1292 */
1293
1294 static int
1295 15673721 basicblock_addop(basicblock *b, int opcode, int oparg,
1296 basicblock *target, const struct location *loc)
1297 {
1298 assert(IS_WITHIN_OPCODE_RANGE(opcode));
1299 assert(!IS_ASSEMBLER_OPCODE(opcode));
1300 assert(HAS_ARG(opcode) || oparg == 0);
1301 assert(0 <= oparg && oparg < (1 << 30));
1302 assert((target == NULL) ||
1303 IS_JUMP_OPCODE(opcode) ||
1304 IS_BLOCK_PUSH_OPCODE(opcode));
1305 assert(oparg == 0 || target == NULL);
1306
1307 15673721 int off = basicblock_next_instr(b);
1308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15673721 times.
15673721 if (off < 0) {
1309 return 0;
1310 }
1311 15673721 struct instr *i = &b->b_instr[off];
1312 15673721 i->i_opcode = opcode;
1313 15673721 i->i_oparg = oparg;
1314 15673721 i->i_target = target;
1315
2/2
✓ Branch 0 taken 15591786 times.
✓ Branch 1 taken 81935 times.
15673721 i->i_loc = loc ? *loc : NO_LOCATION;
1316
1317 15673721 return 1;
1318 }
1319
1320 static int
1321 2377771 compiler_addop(struct compiler *c, int opcode, bool line)
1322 {
1323 assert(!HAS_ARG(opcode) || IS_ARTIFICIAL(opcode));
1324
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2377771 times.
2377771 if (compiler_use_new_implicit_block_if_needed(c) < 0) {
1325 return -1;
1326 }
1327
1328
2/2
✓ Branch 0 taken 2364406 times.
✓ Branch 1 taken 13365 times.
2377771 const struct location *loc = line ? &c->u->u_loc : NULL;
1329 2377771 return basicblock_addop(c->u->u_curblock, opcode, 0, NULL, loc);
1330 }
1331
1332 static Py_ssize_t
1333 8638041 compiler_add_o(PyObject *dict, PyObject *o)
1334 {
1335 PyObject *v;
1336 Py_ssize_t arg;
1337
1338 8638041 v = PyDict_GetItemWithError(dict, o);
1339
2/2
✓ Branch 0 taken 3100589 times.
✓ Branch 1 taken 5537452 times.
8638041 if (!v) {
1340
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3100589 times.
3100589 if (PyErr_Occurred()) {
1341 return -1;
1342 }
1343 3100589 arg = PyDict_GET_SIZE(dict);
1344 3100589 v = PyLong_FromSsize_t(arg);
1345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3100589 times.
3100589 if (!v) {
1346 return -1;
1347 }
1348
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3100589 times.
3100589 if (PyDict_SetItem(dict, o, v) < 0) {
1349 Py_DECREF(v);
1350 return -1;
1351 }
1352 3100589 Py_DECREF(v);
1353 }
1354 else
1355 5537452 arg = PyLong_AsLong(v);
1356 8638041 return arg;
1357 }
1358
1359 // Merge const *o* recursively and return constant key object.
1360 static PyObject*
1361 2956911 merge_consts_recursive(PyObject *const_cache, PyObject *o)
1362 {
1363 assert(PyDict_CheckExact(const_cache));
1364 // None and Ellipsis are singleton, and key is the singleton.
1365 // No need to merge object and key.
1366
4/4
✓ Branch 0 taken 2475354 times.
✓ Branch 1 taken 481557 times.
✓ Branch 2 taken 432 times.
✓ Branch 3 taken 2474922 times.
2956911 if (o == Py_None || o == Py_Ellipsis) {
1367 481989 Py_INCREF(o);
1368 481989 return o;
1369 }
1370
1371 2474922 PyObject *key = _PyCode_ConstantKey(o);
1372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2474922 times.
2474922 if (key == NULL) {
1373 return NULL;
1374 }
1375
1376 // t is borrowed reference
1377 2474922 PyObject *t = PyDict_SetDefault(const_cache, key, key);
1378
2/2
✓ Branch 0 taken 275858 times.
✓ Branch 1 taken 2199064 times.
2474922 if (t != key) {
1379 // o is registered in const_cache. Just use it.
1380 275858 Py_XINCREF(t);
1381 275858 Py_DECREF(key);
1382 275858 return t;
1383 }
1384
1385 // We registered o in const_cache.
1386 // When o is a tuple or frozenset, we want to merge its
1387 // items too.
1388
2/2
✓ Branch 1 taken 100052 times.
✓ Branch 2 taken 2099012 times.
2199064 if (PyTuple_CheckExact(o)) {
1389 100052 Py_ssize_t len = PyTuple_GET_SIZE(o);
1390
2/2
✓ Branch 0 taken 410937 times.
✓ Branch 1 taken 100052 times.
510989 for (Py_ssize_t i = 0; i < len; i++) {
1391 410937 PyObject *item = PyTuple_GET_ITEM(o, i);
1392 410937 PyObject *u = merge_consts_recursive(const_cache, item);
1393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 410937 times.
410937 if (u == NULL) {
1394 Py_DECREF(key);
1395 return NULL;
1396 }
1397
1398 // See _PyCode_ConstantKey()
1399 PyObject *v; // borrowed
1400
2/2
✓ Branch 1 taken 27268 times.
✓ Branch 2 taken 383669 times.
410937 if (PyTuple_CheckExact(u)) {
1401 27268 v = PyTuple_GET_ITEM(u, 1);
1402 }
1403 else {
1404 383669 v = u;
1405 }
1406
2/2
✓ Branch 0 taken 25045 times.
✓ Branch 1 taken 385892 times.
410937 if (v != item) {
1407 25045 Py_INCREF(v);
1408 25045 PyTuple_SET_ITEM(o, i, v);
1409 25045 Py_DECREF(item);
1410 }
1411
1412 410937 Py_DECREF(u);
1413 }
1414 }
1415
2/2
✓ Branch 1 taken 297 times.
✓ Branch 2 taken 2098715 times.
2099012 else if (PyFrozenSet_CheckExact(o)) {
1416 // *key* is tuple. And its first item is frozenset of
1417 // constant keys.
1418 // See _PyCode_ConstantKey() for detail.
1419 assert(PyTuple_CheckExact(key));
1420 assert(PyTuple_GET_SIZE(key) == 2);
1421
1422 297 Py_ssize_t len = PySet_GET_SIZE(o);
1423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (len == 0) { // empty frozenset should not be re-created.
1424 return key;
1425 }
1426 297 PyObject *tuple = PyTuple_New(len);
1427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (tuple == NULL) {
1428 Py_DECREF(key);
1429 return NULL;
1430 }
1431 297 Py_ssize_t i = 0, pos = 0;
1432 PyObject *item;
1433 Py_hash_t hash;
1434
2/2
✓ Branch 1 taken 1852 times.
✓ Branch 2 taken 297 times.
2149 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1435 1852 PyObject *k = merge_consts_recursive(const_cache, item);
1436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1852 times.
1852 if (k == NULL) {
1437 Py_DECREF(tuple);
1438 Py_DECREF(key);
1439 return NULL;
1440 }
1441 PyObject *u;
1442
2/2
✓ Branch 1 taken 110 times.
✓ Branch 2 taken 1742 times.
1852 if (PyTuple_CheckExact(k)) {
1443 110 u = PyTuple_GET_ITEM(k, 1);
1444 110 Py_INCREF(u);
1445 110 Py_DECREF(k);
1446 }
1447 else {
1448 1742 u = k;
1449 }
1450 1852 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1451 1852 i++;
1452 }
1453
1454 // Instead of rewriting o, we create new frozenset and embed in the
1455 // key tuple. Caller should get merged frozenset from the key tuple.
1456 297 PyObject *new = PyFrozenSet_New(tuple);
1457 297 Py_DECREF(tuple);
1458
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 297 times.
297 if (new == NULL) {
1459 Py_DECREF(key);
1460 return NULL;
1461 }
1462 assert(PyTuple_GET_ITEM(key, 1) == o);
1463 297 Py_DECREF(o);
1464 297 PyTuple_SET_ITEM(key, 1, new);
1465 }
1466
1467 2199064 return key;
1468 }
1469
1470 static Py_ssize_t
1471 2544122 compiler_add_const(struct compiler *c, PyObject *o)
1472 {
1473 2544122 PyObject *key = merge_consts_recursive(c->c_const_cache, o);
1474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2544122 times.
2544122 if (key == NULL) {
1475 return -1;
1476 }
1477
1478 2544122 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
1479 2544122 Py_DECREF(key);
1480 2544122 return arg;
1481 }
1482
1483 static int
1484 2229512 compiler_addop_load_const(struct compiler *c, PyObject *o)
1485 {
1486 2229512 Py_ssize_t arg = compiler_add_const(c, o);
1487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2229512 times.
2229512 if (arg < 0)
1488 return 0;
1489 2229512 return compiler_addop_i(c, LOAD_CONST, arg, true);
1490 }
1491
1492 static int
1493 2745175 compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
1494 PyObject *o)
1495 {
1496 2745175 Py_ssize_t arg = compiler_add_o(dict, o);
1497
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2745175 times.
2745175 if (arg < 0)
1498 return 0;
1499 2745175 return compiler_addop_i(c, opcode, arg, true);
1500 }
1501
1502 static int
1503 919167 compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
1504 PyObject *o)
1505 {
1506 Py_ssize_t arg;
1507
1508 919167 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 919167 times.
919167 if (!mangled)
1510 return 0;
1511 919167 arg = compiler_add_o(dict, mangled);
1512 919167 Py_DECREF(mangled);
1513
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 919167 times.
919167 if (arg < 0)
1514 return 0;
1515
2/2
✓ Branch 0 taken 450095 times.
✓ Branch 1 taken 469072 times.
919167 if (opcode == LOAD_ATTR) {
1516 450095 arg <<= 1;
1517 }
1518
2/2
✓ Branch 0 taken 258177 times.
✓ Branch 1 taken 660990 times.
919167 if (opcode == LOAD_METHOD) {
1519 258177 opcode = LOAD_ATTR;
1520 258177 arg <<= 1;
1521 258177 arg |= 1;
1522 }
1523 919167 return compiler_addop_i(c, opcode, arg, true);
1524 }
1525
1526 /* Add an opcode with an integer argument.
1527 Returns 0 on failure, 1 on success.
1528 */
1529 static int
1530 12777944 compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg, bool line)
1531 {
1532
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12777944 times.
12777944 if (compiler_use_new_implicit_block_if_needed(c) < 0) {
1533 return -1;
1534 }
1535 /* oparg value is unsigned, but a signed C int is usually used to store
1536 it in the C code (like Python/ceval.c).
1537
1538 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1539
1540 The argument of a concrete bytecode instruction is limited to 8-bit.
1541 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1542
1543 12777944 int oparg_ = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1544
1545
2/2
✓ Branch 0 taken 12773642 times.
✓ Branch 1 taken 4302 times.
12777944 const struct location *loc = line ? &c->u->u_loc : NULL;
1546 12777944 return basicblock_addop(c->u->u_curblock, opcode, oparg_, NULL, loc);
1547 }
1548
1549 static int
1550 517984 compiler_addop_j(struct compiler *c, int opcode, basicblock *target, bool line)
1551 {
1552
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 517984 times.
517984 if (compiler_use_new_implicit_block_if_needed(c) < 0) {
1553 return -1;
1554 }
1555
2/2
✓ Branch 0 taken 453716 times.
✓ Branch 1 taken 64268 times.
517984 const struct location *loc = line ? &c->u->u_loc : NULL;
1556 assert(target != NULL);
1557 assert(IS_JUMP_OPCODE(opcode) || IS_BLOCK_PUSH_OPCODE(opcode));
1558 517984 return basicblock_addop(c->u->u_curblock, opcode, 0, target, loc);
1559 }
1560
1561 #define ADDOP(C, OP) { \
1562 if (!compiler_addop((C), (OP), true)) \
1563 return 0; \
1564 }
1565
1566 #define ADDOP_NOLINE(C, OP) { \
1567 if (!compiler_addop((C), (OP), false)) \
1568 return 0; \
1569 }
1570
1571 #define ADDOP_IN_SCOPE(C, OP) { \
1572 if (!compiler_addop((C), (OP), true)) { \
1573 compiler_exit_scope(c); \
1574 return 0; \
1575 } \
1576 }
1577
1578 #define ADDOP_LOAD_CONST(C, O) { \
1579 if (!compiler_addop_load_const((C), (O))) \
1580 return 0; \
1581 }
1582
1583 /* Same as ADDOP_LOAD_CONST, but steals a reference. */
1584 #define ADDOP_LOAD_CONST_NEW(C, O) { \
1585 PyObject *__new_const = (O); \
1586 if (__new_const == NULL) { \
1587 return 0; \
1588 } \
1589 if (!compiler_addop_load_const((C), __new_const)) { \
1590 Py_DECREF(__new_const); \
1591 return 0; \
1592 } \
1593 Py_DECREF(__new_const); \
1594 }
1595
1596 #define ADDOP_N(C, OP, O, TYPE) { \
1597 assert(!HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
1598 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1599 Py_DECREF((O)); \
1600 return 0; \
1601 } \
1602 Py_DECREF((O)); \
1603 }
1604
1605 #define ADDOP_NAME(C, OP, O, TYPE) { \
1606 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1607 return 0; \
1608 }
1609
1610 #define ADDOP_I(C, OP, O) { \
1611 if (!compiler_addop_i((C), (OP), (O), true)) \
1612 return 0; \
1613 }
1614
1615 #define ADDOP_I_NOLINE(C, OP, O) { \
1616 if (!compiler_addop_i((C), (OP), (O), false)) \
1617 return 0; \
1618 }
1619
1620 #define ADDOP_JUMP(C, OP, O) { \
1621 if (!compiler_addop_j((C), (OP), (O), true)) \
1622 return 0; \
1623 }
1624
1625 /* Add a jump with no line number.
1626 * Used for artificial jumps that have no corresponding
1627 * token in the source code. */
1628 #define ADDOP_JUMP_NOLINE(C, OP, O) { \
1629 if (!compiler_addop_j((C), (OP), (O), false)) \
1630 return 0; \
1631 }
1632
1633 #define ADDOP_COMPARE(C, CMP) { \
1634 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1635 return 0; \
1636 }
1637
1638 #define ADDOP_BINARY(C, BINOP) \
1639 RETURN_IF_FALSE(addop_binary((C), (BINOP), false))
1640
1641 #define ADDOP_INPLACE(C, BINOP) \
1642 RETURN_IF_FALSE(addop_binary((C), (BINOP), true))
1643
1644 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1645 the ASDL name to synthesize the name of the C type and the visit function.
1646 */
1647
1648 #define ADD_YIELD_FROM(C, await) \
1649 RETURN_IF_FALSE(compiler_add_yield_from((C), (await)))
1650
1651 #define POP_EXCEPT_AND_RERAISE(C) \
1652 RETURN_IF_FALSE(compiler_pop_except_and_reraise((C)))
1653
1654 #define ADDOP_YIELD(C) \
1655 RETURN_IF_FALSE(addop_yield(C))
1656
1657 #define VISIT(C, TYPE, V) {\
1658 if (!compiler_visit_ ## TYPE((C), (V))) \
1659 return 0; \
1660 }
1661
1662 #define VISIT_IN_SCOPE(C, TYPE, V) {\
1663 if (!compiler_visit_ ## TYPE((C), (V))) { \
1664 compiler_exit_scope(c); \
1665 return 0; \
1666 } \
1667 }
1668
1669 #define VISIT_SEQ(C, TYPE, SEQ) { \
1670 int _i; \
1671 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
1672 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1673 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1674 if (!compiler_visit_ ## TYPE((C), elt)) \
1675 return 0; \
1676 } \
1677 }
1678
1679 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1680 int _i; \
1681 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
1682 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1683 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1684 if (!compiler_visit_ ## TYPE((C), elt)) { \
1685 compiler_exit_scope(c); \
1686 return 0; \
1687 } \
1688 } \
1689 }
1690
1691 #define RETURN_IF_FALSE(X) \
1692 if (!(X)) { \
1693 return 0; \
1694 }
1695
1696 static int
1697 448800 compiler_enter_scope(struct compiler *c, identifier name,
1698 int scope_type, void *key, int lineno)
1699 {
1700 struct compiler_unit *u;
1701 basicblock *block;
1702
1703 448800 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
1704 struct compiler_unit));
1705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (!u) {
1706 PyErr_NoMemory();
1707 return 0;
1708 }
1709 448800 u->u_scope_type = scope_type;
1710 448800 u->u_argcount = 0;
1711 448800 u->u_posonlyargcount = 0;
1712 448800 u->u_kwonlyargcount = 0;
1713 448800 u->u_ste = PySymtable_Lookup(c->c_st, key);
1714
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (!u->u_ste) {
1715 compiler_unit_free(u);
1716 return 0;
1717 }
1718 448800 Py_INCREF(name);
1719 448800 u->u_name = name;
1720 448800 u->u_varnames = list2dict(u->u_ste->ste_varnames);
1721 448800 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
1722
2/4
✓ Branch 0 taken 448800 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 448800 times.
448800 if (!u->u_varnames || !u->u_cellvars) {
1723 compiler_unit_free(u);
1724 return 0;
1725 }
1726
2/2
✓ Branch 0 taken 2701 times.
✓ Branch 1 taken 446099 times.
448800 if (u->u_ste->ste_needs_class_closure) {
1727 /* Cook up an implicit __class__ cell. */
1728 int res;
1729 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
1730 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
1731 2701 res = PyDict_SetItem(u->u_cellvars, &_Py_ID(__class__),
1732 _PyLong_GetZero());
1733
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2701 times.
2701 if (res < 0) {
1734 compiler_unit_free(u);
1735 return 0;
1736 }
1737 }
1738
1739 448800 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1740 PyDict_GET_SIZE(u->u_cellvars));
1741
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (!u->u_freevars) {
1742 compiler_unit_free(u);
1743 return 0;
1744 }
1745
1746 448800 u->u_blocks = NULL;
1747 448800 u->u_nfblocks = 0;
1748 448800 u->u_firstlineno = lineno;
1749 448800 u->u_loc = LOCATION(lineno, lineno, 0, 0);
1750 448800 u->u_consts = PyDict_New();
1751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (!u->u_consts) {
1752 compiler_unit_free(u);
1753 return 0;
1754 }
1755 448800 u->u_names = PyDict_New();
1756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (!u->u_names) {
1757 compiler_unit_free(u);
1758 return 0;
1759 }
1760
1761 448800 u->u_private = NULL;
1762
1763 /* Push the old compiler_unit on the stack. */
1764
2/2
✓ Branch 0 taken 292115 times.
✓ Branch 1 taken 156685 times.
448800 if (c->u) {
1765 292115 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
1766
2/4
✓ Branch 0 taken 292115 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 292115 times.
292115 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
1767 Py_XDECREF(capsule);
1768 compiler_unit_free(u);
1769 return 0;
1770 }
1771 292115 Py_DECREF(capsule);
1772 292115 u->u_private = c->u->u_private;
1773 292115 Py_XINCREF(u->u_private);
1774 }
1775 448800 c->u = u;
1776
1777 448800 c->c_nestlevel++;
1778
1779 448800 block = compiler_new_block(c);
1780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (block == NULL)
1781 return 0;
1782 448800 c->u->u_curblock = block;
1783
1784
2/2
✓ Branch 0 taken 156685 times.
✓ Branch 1 taken 292115 times.
448800 if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
1785 156685 c->u->u_loc.lineno = -1;
1786 }
1787 else {
1788
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 292115 times.
292115 if (!compiler_set_qualname(c))
1789 return 0;
1790 }
1791
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 ADDOP_I(c, RESUME, 0);
1792
1793 448800 return 1;
1794 }
1795
1796 static void
1797 448800 compiler_exit_scope(struct compiler *c)
1798 {
1799 // Don't call PySequence_DelItem() with an exception raised
1800 PyObject *exc_type, *exc_val, *exc_tb;
1801 448800 PyErr_Fetch(&exc_type, &exc_val, &exc_tb);
1802
1803 448800 c->c_nestlevel--;
1804 448800 compiler_unit_free(c->u);
1805 /* Restore c->u to the parent unit. */
1806 448800 Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
1807
2/2
✓ Branch 0 taken 292115 times.
✓ Branch 1 taken 156685 times.
448800 if (n >= 0) {
1808 292115 PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
1809 292115 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
1810 assert(c->u);
1811 /* we are deleting from a list so this really shouldn't fail */
1812
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 292115 times.
292115 if (PySequence_DelItem(c->c_stack, n) < 0) {
1813 _PyErr_WriteUnraisableMsg("on removing the last compiler "
1814 "stack item", NULL);
1815 }
1816 292115 compiler_unit_check(c->u);
1817 }
1818 else {
1819 156685 c->u = NULL;
1820 }
1821
1822 448800 PyErr_Restore(exc_type, exc_val, exc_tb);
1823 448800 }
1824
1825 /* Search if variable annotations are present statically in a block. */
1826
1827 static int
1828 76109 find_ann(asdl_stmt_seq *stmts)
1829 {
1830 76109 int i, j, res = 0;
1831 stmt_ty st;
1832
1833
4/4
✓ Branch 0 taken 379936 times.
✓ Branch 1 taken 8508 times.
✓ Branch 2 taken 312828 times.
✓ Branch 3 taken 75616 times.
388444 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1834 312828 st = (stmt_ty)asdl_seq_GET(stmts, i);
1835
7/10
✓ Branch 0 taken 487 times.
✓ Branch 1 taken 647 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
✓ Branch 4 taken 6438 times.
✓ Branch 5 taken 93 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1492 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 303661 times.
312828 switch (st->kind) {
1836 487 case AnnAssign_kind:
1837 487 return 1;
1838 647 case For_kind:
1839
2/4
✓ Branch 1 taken 647 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 647 times.
1294 res = find_ann(st->v.For.body) ||
1840 647 find_ann(st->v.For.orelse);
1841 647 break;
1842 case AsyncFor_kind:
1843 res = find_ann(st->v.AsyncFor.body) ||
1844 find_ann(st->v.AsyncFor.orelse);
1845 break;
1846 10 case While_kind:
1847
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
20 res = find_ann(st->v.While.body) ||
1848 10 find_ann(st->v.While.orelse);
1849 10 break;
1850 6438 case If_kind:
1851
3/4
✓ Branch 1 taken 6435 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6435 times.
12873 res = find_ann(st->v.If.body) ||
1852 6435 find_ann(st->v.If.orelse);
1853 6438 break;
1854 93 case With_kind:
1855 93 res = find_ann(st->v.With.body);
1856 93 break;
1857 case AsyncWith_kind:
1858 res = find_ann(st->v.AsyncWith.body);
1859 break;
1860 1492 case Try_kind:
1861
4/4
✓ Branch 0 taken 2946 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 1477 times.
✓ Branch 3 taken 1492 times.
2969 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1862 1477 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1863 st->v.Try.handlers, j);
1864
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1477 times.
1477 if (find_ann(handler->v.ExceptHandler.body)) {
1865 return 1;
1866 }
1867 }
1868
1/2
✓ Branch 1 taken 1492 times.
✗ Branch 2 not taken.
2984 res = find_ann(st->v.Try.body) ||
1869
2/4
✓ Branch 0 taken 1492 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1492 times.
2984 find_ann(st->v.Try.finalbody) ||
1870 1492 find_ann(st->v.Try.orelse);
1871 1492 break;
1872 case TryStar_kind:
1873 for (j = 0; j < asdl_seq_LEN(st->v.TryStar.handlers); j++) {
1874 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1875 st->v.TryStar.handlers, j);
1876 if (find_ann(handler->v.ExceptHandler.body)) {
1877 return 1;
1878 }
1879 }
1880 res = find_ann(st->v.TryStar.body) ||
1881 find_ann(st->v.TryStar.finalbody) ||
1882 find_ann(st->v.TryStar.orelse);
1883 break;
1884 303661 default:
1885 303661 res = 0;
1886 }
1887
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 312335 times.
312341 if (res) {
1888 6 break;
1889 }
1890 }
1891 75622 return res;
1892 }
1893
1894 /*
1895 * Frame block handling functions
1896 */
1897
1898 static int
1899 81264 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
1900 basicblock *exit, void *datum)
1901 {
1902 struct fblockinfo *f;
1903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 81264 times.
81264 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
1904 return compiler_error(c, "too many statically nested blocks");
1905 }
1906 81264 f = &c->u->u_fblock[c->u->u_nfblocks++];
1907 81264 f->fb_type = t;
1908 81264 f->fb_block = b;
1909 81264 f->fb_exit = exit;
1910 81264 f->fb_datum = datum;
1911 81264 return 1;
1912 }
1913
1914 static void
1915 81264 compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1916 {
1917 81264 struct compiler_unit *u = c->u;
1918 assert(u->u_nfblocks > 0);
1919 81264 u->u_nfblocks--;
1920 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1921 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1922 81264 }
1923
1924 static int
1925 5072 compiler_call_exit_with_nones(struct compiler *c) {
1926
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5072 times.
5072 ADDOP_LOAD_CONST(c, Py_None);
1927
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5072 times.
5072 ADDOP_LOAD_CONST(c, Py_None);
1928
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5072 times.
5072 ADDOP_LOAD_CONST(c, Py_None);
1929
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5072 times.
5072 ADDOP_I(c, CALL, 2);
1930 5072 return 1;
1931 }
1932
1933 static int
1934 1665 compiler_add_yield_from(struct compiler *c, int await)
1935 {
1936 basicblock *start, *resume, *exit;
1937 1665 start = compiler_new_block(c);
1938 1665 resume = compiler_new_block(c);
1939 1665 exit = compiler_new_block(c);
1940
3/6
✓ Branch 0 taken 1665 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1665 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1665 times.
1665 if (start == NULL || resume == NULL || exit == NULL) {
1941 return 0;
1942 }
1943 1665 compiler_use_next_block(c, start);
1944
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1665 times.
1665 ADDOP_JUMP(c, SEND, exit);
1945 1665 compiler_use_next_block(c, resume);
1946
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1665 times.
1665 ADDOP_I(c, YIELD_VALUE, 0);
1947
3/4
✓ Branch 0 taken 1119 times.
✓ Branch 1 taken 546 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1665 times.
1665 ADDOP_I(c, RESUME, await ? 3 : 2);
1948
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1665 times.
1665 ADDOP_JUMP(c, JUMP_NO_INTERRUPT, start);
1949 1665 compiler_use_next_block(c, exit);
1950 1665 return 1;
1951 }
1952
1953 static int
1954 17727 compiler_pop_except_and_reraise(struct compiler *c)
1955 {
1956 /* Stack contents
1957 * [exc_info, lasti, exc] COPY 3
1958 * [exc_info, lasti, exc, exc_info] POP_EXCEPT
1959 * [exc_info, lasti, exc] RERAISE 1
1960 * (exception_unwind clears the stack)
1961 */
1962
1963
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17727 times.
17727 ADDOP_I(c, COPY, 3);
1964
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17727 times.
17727 ADDOP(c, POP_EXCEPT);
1965
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17727 times.
17727 ADDOP_I(c, RERAISE, 1);
1966 17727 return 1;
1967 }
1968
1969 /* Unwind a frame block. If preserve_tos is true, the TOS before
1970 * popping the blocks will be restored afterwards, unless another
1971 * return, break or continue is found. In which case, the TOS will
1972 * be popped.
1973 */
1974 static int
1975 16764 compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1976 int preserve_tos)
1977 {
1978
7/9
✓ Branch 0 taken 5640 times.
✓ Branch 1 taken 3861 times.
✓ Branch 2 taken 2292 times.
✓ Branch 3 taken 600 times.
✓ Branch 4 taken 10 times.
✓ Branch 5 taken 710 times.
✓ Branch 6 taken 3651 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
16764 switch (info->fb_type) {
1979 5640 case WHILE_LOOP:
1980 case EXCEPTION_HANDLER:
1981 case EXCEPTION_GROUP_HANDLER:
1982 case ASYNC_COMPREHENSION_GENERATOR:
1983 5640 return 1;
1984
1985 3861 case FOR_LOOP:
1986 /* Pop the iterator */
1987
2/2
✓ Branch 0 taken 1306 times.
✓ Branch 1 taken 2555 times.
3861 if (preserve_tos) {
1988
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1306 times.
1306 ADDOP_I(c, SWAP, 2);
1989 }
1990
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3861 times.
3861 ADDOP(c, POP_TOP);
1991 3861 return 1;
1992
1993 2292 case TRY_EXCEPT:
1994
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2292 times.
2292 ADDOP(c, POP_BLOCK);
1995 2292 return 1;
1996
1997 600 case FINALLY_TRY:
1998 /* This POP_BLOCK gets the line number of the unwinding statement */
1999
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 600 times.
600 ADDOP(c, POP_BLOCK);
2000
2/2
✓ Branch 0 taken 241 times.
✓ Branch 1 taken 359 times.
600 if (preserve_tos) {
2001
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 241 times.
241 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
2002 return 0;
2003 }
2004 }
2005 /* Emit the finally block */
2006
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 664 times.
✓ Branch 3 taken 1264 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 664 times.
✓ Branch 6 taken 600 times.
1264 VISIT_SEQ(c, stmt, info->fb_datum);
2007
2/2
✓ Branch 0 taken 241 times.
✓ Branch 1 taken 359 times.
600 if (preserve_tos) {
2008 241 compiler_pop_fblock(c, POP_VALUE, NULL);
2009 }
2010 /* The finally block should appear to execute after the
2011 * statement causing the unwinding, so make the unwinding
2012 * instruction artificial */
2013 600 UNSET_LOC(c);
2014 600 return 1;
2015
2016 10 case FINALLY_END:
2017
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
10 if (preserve_tos) {
2018
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 ADDOP_I(c, SWAP, 2);
2019 }
2020
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 ADDOP(c, POP_TOP); /* exc_value */
2021
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
10 if (preserve_tos) {
2022
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 ADDOP_I(c, SWAP, 2);
2023 }
2024
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 ADDOP(c, POP_BLOCK);
2025
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 ADDOP(c, POP_EXCEPT);
2026 10 return 1;
2027
2028 710 case WITH:
2029 case ASYNC_WITH:
2030 710 SET_LOC(c, (stmt_ty)info->fb_datum);
2031
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 710 times.
710 ADDOP(c, POP_BLOCK);
2032
2/2
✓ Branch 0 taken 571 times.
✓ Branch 1 taken 139 times.
710 if (preserve_tos) {
2033
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 571 times.
571 ADDOP_I(c, SWAP, 2);
2034 }
2035
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 710 times.
710 if(!compiler_call_exit_with_nones(c)) {
2036 return 0;
2037 }
2038
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 704 times.
710 if (info->fb_type == ASYNC_WITH) {
2039
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 ADDOP_I(c, GET_AWAITABLE, 2);
2040
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 ADDOP_LOAD_CONST(c, Py_None);
2041
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 ADD_YIELD_FROM(c, 1);
2042 }
2043
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 710 times.
710 ADDOP(c, POP_TOP);
2044 /* The exit block should appear to execute after the
2045 * statement causing the unwinding, so make the unwinding
2046 * instruction artificial */
2047 710 UNSET_LOC(c);
2048 710 return 1;
2049
2050 3651 case HANDLER_CLEANUP:
2051
2/2
✓ Branch 0 taken 356 times.
✓ Branch 1 taken 3295 times.
3651 if (info->fb_datum) {
2052
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 356 times.
356 ADDOP(c, POP_BLOCK);
2053 }
2054
2/2
✓ Branch 0 taken 1884 times.
✓ Branch 1 taken 1767 times.
3651 if (preserve_tos) {
2055
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1884 times.
1884 ADDOP_I(c, SWAP, 2);
2056 }
2057
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3651 times.
3651 ADDOP(c, POP_BLOCK);
2058
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3651 times.
3651 ADDOP(c, POP_EXCEPT);
2059
2/2
✓ Branch 0 taken 356 times.
✓ Branch 1 taken 3295 times.
3651 if (info->fb_datum) {
2060
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 356 times.
356 ADDOP_LOAD_CONST(c, Py_None);
2061 356 compiler_nameop(c, info->fb_datum, Store);
2062 356 compiler_nameop(c, info->fb_datum, Del);
2063 }
2064 3651 return 1;
2065
2066 case POP_VALUE:
2067 if (preserve_tos) {
2068 ADDOP_I(c, SWAP, 2);
2069 }
2070 ADDOP(c, POP_TOP);
2071 return 1;
2072 }
2073 Py_UNREACHABLE();
2074 }
2075
2076 /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
2077 static int
2078 170245 compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
2079
2/2
✓ Branch 0 taken 150682 times.
✓ Branch 1 taken 19563 times.
170245 if (c->u->u_nfblocks == 0) {
2080 150682 return 1;
2081 }
2082 19563 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
2083
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19563 times.
19563 if (top->fb_type == EXCEPTION_GROUP_HANDLER) {
2084 return compiler_error(
2085 c, "'break', 'continue' and 'return' cannot appear in an except* block");
2086 }
2087
6/6
✓ Branch 0 taken 6622 times.
✓ Branch 1 taken 12941 times.
✓ Branch 2 taken 4986 times.
✓ Branch 3 taken 1636 times.
✓ Branch 4 taken 3927 times.
✓ Branch 5 taken 1059 times.
19563 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
2088 5563 *loop = top;
2089 5563 return 1;
2090 }
2091 14000 struct fblockinfo copy = *top;
2092 14000 c->u->u_nfblocks--;
2093
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14000 times.
14000 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
2094 return 0;
2095 }
2096
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14000 times.
14000 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
2097 return 0;
2098 }
2099 14000 c->u->u_fblock[c->u->u_nfblocks] = copy;
2100 14000 c->u->u_nfblocks++;
2101 14000 return 1;
2102 }
2103
2104 /* Compile a sequence of statements, checking for a docstring
2105 and for annotations. */
2106
2107 static int
2108 55876 compiler_body(struct compiler *c, asdl_stmt_seq *stmts)
2109 {
2110 55876 int i = 0;
2111 stmt_ty st;
2112 PyObject *docstring;
2113
2114 /* Set current line number to the line number of first statement.
2115 This way line number for SETUP_ANNOTATIONS will always
2116 coincide with the line number of first "real" statement in module.
2117 If body is empty, then lineno will be set later in assemble. */
2118
5/6
✓ Branch 0 taken 40672 times.
✓ Branch 1 taken 15204 times.
✓ Branch 2 taken 40383 times.
✓ Branch 3 taken 289 times.
✓ Branch 4 taken 40383 times.
✗ Branch 5 not taken.
55876 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
2119 40383 st = (stmt_ty)asdl_seq_GET(stmts, 0);
2120 40383 SET_LOC(c, st);
2121 }
2122 /* Every annotated class and module should have __annotations__. */
2123
2/2
✓ Branch 1 taken 487 times.
✓ Branch 2 taken 55389 times.
55876 if (find_ann(stmts)) {
2124
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 487 times.
487 ADDOP(c, SETUP_ANNOTATIONS);
2125 }
2126
3/4
✓ Branch 0 taken 55587 times.
✓ Branch 1 taken 289 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 55587 times.
55876 if (!asdl_seq_LEN(stmts))
2127 289 return 1;
2128 /* if not -OO mode, set docstring */
2129
1/2
✓ Branch 0 taken 55587 times.
✗ Branch 1 not taken.
55587 if (c->c_optimize < 2) {
2130 55587 docstring = _PyAST_GetDocString(stmts);
2131
2/2
✓ Branch 0 taken 10742 times.
✓ Branch 1 taken 44845 times.
55587 if (docstring) {
2132 10742 i = 1;
2133 10742 st = (stmt_ty)asdl_seq_GET(stmts, 0);
2134 assert(st->kind == Expr_kind);
2135
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10742 times.
10742 VISIT(c, expr, st->v.Expr.value);
2136 10742 UNSET_LOC(c);
2137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10742 times.
10742 if (!compiler_nameop(c, &_Py_ID(__doc__), Store))
2138 return 0;
2139 }
2140 }
2141
3/4
✓ Branch 0 taken 328391 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 272804 times.
✓ Branch 3 taken 55587 times.
328391 for (; i < asdl_seq_LEN(stmts); i++)
2142
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 272804 times.
272804 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
2143 55587 return 1;
2144 }
2145
2146 static PyCodeObject *
2147 156685 compiler_mod(struct compiler *c, mod_ty mod)
2148 {
2149 PyCodeObject *co;
2150 156685 int addNone = 1;
2151 _Py_DECLARE_STR(anon_module, "<module>");
2152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 156685 times.
156685 if (!compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE,
2153 mod, 1)) {
2154 return NULL;
2155 }
2156 156685 c->u->u_loc.lineno = 1;
2157
2/4
✓ Branch 0 taken 40672 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 116013 times.
✗ Branch 3 not taken.
156685 switch (mod->kind) {
2158 40672 case Module_kind:
2159
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 40672 times.
40672 if (!compiler_body(c, mod->v.Module.body)) {
2160 compiler_exit_scope(c);
2161 return 0;
2162 }
2163 40672 break;
2164 case Interactive_kind:
2165 if (find_ann(mod->v.Interactive.body)) {
2166 ADDOP(c, SETUP_ANNOTATIONS);
2167 }
2168 c->c_interactive = 1;
2169 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
2170 break;
2171 116013 case Expression_kind:
2172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 116013 times.
116013 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
2173 116013 addNone = 0;
2174 116013 break;
2175 default:
2176 PyErr_Format(PyExc_SystemError,
2177 "module kind %d should not be possible",
2178 mod->kind);
2179 return 0;
2180 }
2181 156685 co = assemble(c, addNone);
2182 156685 compiler_exit_scope(c);
2183 156685 return co;
2184 }
2185
2186 /* The test for LOCAL must come before the test for FREE in order to
2187 handle classes where name is both local and free. The local var is
2188 a method and the free var is a free var referenced within a method.
2189 */
2190
2191 static int
2192 27419 get_ref_type(struct compiler *c, PyObject *name)
2193 {
2194 int scope;
2195
4/4
✓ Branch 0 taken 4223 times.
✓ Branch 1 taken 23196 times.
✓ Branch 2 taken 3863 times.
✓ Branch 3 taken 360 times.
31642 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
2196 4223 _PyUnicode_EqualToASCIIString(name, "__class__"))
2197 3863 return CELL;
2198 23556 scope = _PyST_GetScope(c->u->u_ste, name);
2199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 23556 times.
23556 if (scope == 0) {
2200 PyErr_Format(PyExc_SystemError,
2201 "_PyST_GetScope(name=%R) failed: "
2202 "unknown scope in unit %S (%R); "
2203 "symbols: %R; locals: %R; globals: %R",
2204 name,
2205 c->u->u_name, c->u->u_ste->ste_id,
2206 c->u->u_ste->ste_symbols, c->u->u_varnames, c->u->u_names);
2207 return -1;
2208 }
2209 23556 return scope;
2210 }
2211
2212 static int
2213 30120 compiler_lookup_arg(PyObject *dict, PyObject *name)
2214 {
2215 PyObject *v;
2216 30120 v = PyDict_GetItemWithError(dict, name);
2217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30120 times.
30120 if (v == NULL)
2218 return -1;
2219 30120 return PyLong_AS_LONG(v);
2220 }
2221
2222 static int
2223 292115 compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags,
2224 PyObject *qualname)
2225 {
2226
2/2
✓ Branch 0 taken 15204 times.
✓ Branch 1 taken 276911 times.
292115 if (qualname == NULL)
2227 15204 qualname = co->co_name;
2228
2229
2/2
✓ Branch 0 taken 18367 times.
✓ Branch 1 taken 273748 times.
292115 if (co->co_nfreevars) {
2230 18367 int i = co->co_nlocals + co->co_nplaincellvars;
2231
2/2
✓ Branch 0 taken 27419 times.
✓ Branch 1 taken 18367 times.
45786 for (; i < co->co_nlocalsplus; ++i) {
2232 /* Bypass com_addop_varname because it will generate
2233 LOAD_DEREF but LOAD_CLOSURE is needed.
2234 */
2235 27419 PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
2236
2237 /* Special case: If a class contains a method with a
2238 free variable that has the same name as a method,
2239 the name will be considered free *and* local in the
2240 class. It should be handled by the closure, as
2241 well as by the normal name lookup logic.
2242 */
2243 27419 int reftype = get_ref_type(c, name);
2244
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27419 times.
27419 if (reftype == -1) {
2245 return 0;
2246 }
2247 int arg;
2248
2/2
✓ Branch 0 taken 26318 times.
✓ Branch 1 taken 1101 times.
27419 if (reftype == CELL) {
2249 26318 arg = compiler_lookup_arg(c->u->u_cellvars, name);
2250 }
2251 else {
2252 1101 arg = compiler_lookup_arg(c->u->u_freevars, name);
2253 }
2254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27419 times.
27419 if (arg == -1) {
2255 PyObject *freevars = _PyCode_GetFreevars(co);
2256 if (freevars == NULL) {
2257 PyErr_Clear();
2258 }
2259 PyErr_Format(PyExc_SystemError,
2260 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
2261 "freevars of code %S: %R",
2262 name,
2263 reftype,
2264 c->u->u_name,
2265 co->co_name,
2266 freevars);
2267 Py_DECREF(freevars);
2268 return 0;
2269 }
2270
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 27419 times.
27419 ADDOP_I(c, LOAD_CLOSURE, arg);
2271 }
2272 18367 flags |= 0x08;
2273
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18367 times.
18367 ADDOP_I(c, BUILD_TUPLE, co->co_nfreevars);
2274 }
2275
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 292115 times.
292115 ADDOP_LOAD_CONST(c, (PyObject*)co);
2276
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 292115 times.
292115 ADDOP_I(c, MAKE_FUNCTION, flags);
2277 292115 return 1;
2278 }
2279
2280 static int
2281 175946 compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
2282 {
2283 int i;
2284
2285
2/2
✓ Branch 0 taken 161896 times.
✓ Branch 1 taken 14050 times.
175946 if (!decos)
2286 161896 return 1;
2287
2288
3/4
✓ Branch 0 taken 28602 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14552 times.
✓ Branch 3 taken 14050 times.
28602 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2289
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14552 times.
14552 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
2290 }
2291 14050 return 1;
2292 }
2293
2294 static int
2295 175946 compiler_apply_decorators(struct compiler *c, asdl_expr_seq* decos)
2296 {
2297
2/2
✓ Branch 0 taken 161896 times.
✓ Branch 1 taken 14050 times.
175946 if (!decos)
2298 161896 return 1;
2299
2300 14050 struct location old_loc = c->u->u_loc;
2301
3/4
✓ Branch 0 taken 14050 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14552 times.
✓ Branch 3 taken 14050 times.
28602 for (Py_ssize_t i = asdl_seq_LEN(decos) - 1; i > -1; i--) {
2302 14552 SET_LOC(c, (expr_ty)asdl_seq_GET(decos, i));
2303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14552 times.
14552 ADDOP_I(c, CALL, 0);
2304 }
2305 14050 c->u->u_loc = old_loc;
2306 14050 return 1;
2307 }
2308
2309 static int
2310 260906 compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
2311 asdl_expr_seq *kw_defaults)
2312 {
2313 /* Push a dict of keyword-only default values.
2314
2315 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
2316 */
2317 int i;
2318 260906 PyObject *keys = NULL;
2319
2320
3/4
✓ Branch 0 taken 263930 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3024 times.
✓ Branch 3 taken 260906 times.
263930 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
2321 3024 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
2322 3024 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
2323
2/2
✓ Branch 0 taken 2872 times.
✓ Branch 1 taken 152 times.
3024 if (default_) {
2324 2872 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
2325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2872 times.
2872 if (!mangled) {
2326 goto error;
2327 }
2328
2/2
✓ Branch 0 taken 1022 times.
✓ Branch 1 taken 1850 times.
2872 if (keys == NULL) {
2329 1022 keys = PyList_New(1);
2330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1022 times.
1022 if (keys == NULL) {
2331 Py_DECREF(mangled);
2332 return 0;
2333 }
2334 1022 PyList_SET_ITEM(keys, 0, mangled);
2335 }
2336 else {
2337 1850 int res = PyList_Append(keys, mangled);
2338 1850 Py_DECREF(mangled);
2339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1850 times.
1850 if (res == -1) {
2340 goto error;
2341 }
2342 }
2343
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2872 times.
2872 if (!compiler_visit_expr(c, default_)) {
2344 goto error;
2345 }
2346 }
2347 }
2348
2/2
✓ Branch 0 taken 1022 times.
✓ Branch 1 taken 259884 times.
260906 if (keys != NULL) {
2349 1022 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2350 1022 PyObject *keys_tuple = PyList_AsTuple(keys);
2351 1022 Py_DECREF(keys);
2352
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1022 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1022 times.
1022 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
2353
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1022 times.
1022 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
2354 assert(default_count > 0);
2355 1022 return 1;
2356 }
2357 else {
2358 259884 return -1;
2359 }
2360
2361 error:
2362 Py_XDECREF(keys);
2363 return 0;
2364 }
2365
2366 static int
2367 877 compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2368 {
2369
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 877 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 877 times.
877 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
2370 877 return 1;
2371 }
2372
2373 static int
2374 551036 compiler_visit_argannotation(struct compiler *c, identifier id,
2375 expr_ty annotation, Py_ssize_t *annotations_len)
2376 {
2377
2/2
✓ Branch 0 taken 503955 times.
✓ Branch 1 taken 47081 times.
551036 if (!annotation) {
2378 503955 return 1;
2379 }
2380
2381 47081 PyObject *mangled = _Py_Mangle(c->u->u_private, id);
2382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47081 times.
47081 if (!mangled) {
2383 return 0;
2384 }
2385
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47081 times.
47081 ADDOP_LOAD_CONST(c, mangled);
2386 47081 Py_DECREF(mangled);
2387
2388
2/2
✓ Branch 0 taken 873 times.
✓ Branch 1 taken 46208 times.
47081 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2389
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 873 times.
873 VISIT(c, annexpr, annotation);
2390 }
2391 else {
2392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 46208 times.
46208 if (annotation->kind == Starred_kind) {
2393 // *args: *Ts (where Ts is a TypeVarTuple).
2394 // Do [annotation_value] = [*Ts].
2395 // (Note that in theory we could end up here even for an argument
2396 // other than *args, but in practice the grammar doesn't allow it.)
2397 VISIT(c, expr, annotation->v.Starred.value);
2398 ADDOP_I(c, UNPACK_SEQUENCE, (Py_ssize_t) 1);
2399 }
2400 else {
2401
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46208 times.
46208 VISIT(c, expr, annotation);
2402 }
2403 }
2404 47081 *annotations_len += 2;
2405 47081 return 1;
2406 }
2407
2408 static int
2409 482226 compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
2410 Py_ssize_t *annotations_len)
2411 {
2412 int i;
2413
3/4
✓ Branch 0 taken 872072 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 389846 times.
✓ Branch 3 taken 482226 times.
872072 for (i = 0; i < asdl_seq_LEN(args); i++) {
2414 389846 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
2415
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 389846 times.
389846 if (!compiler_visit_argannotation(
2416 c,
2417 arg->arg,
2418 arg->annotation,
2419 annotations_len))
2420 return 0;
2421 }
2422 482226 return 1;
2423 }
2424
2425 static int
2426 160742 compiler_visit_annotations(struct compiler *c, arguments_ty args,
2427 expr_ty returns)
2428 {
2429 /* Push arg annotation names and values.
2430 The expressions are evaluated out-of-order wrt the source code.
2431
2432 Return 0 on error, -1 if no annotations pushed, 1 if a annotations is pushed.
2433 */
2434 160742 Py_ssize_t annotations_len = 0;
2435
2436
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 160742 times.
160742 if (!compiler_visit_argannotations(c, args->args, &annotations_len))
2437 return 0;
2438
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 160742 times.
160742 if (!compiler_visit_argannotations(c, args->posonlyargs, &annotations_len))
2439 return 0;
2440
5/6
✓ Branch 0 taken 7246 times.
✓ Branch 1 taken 153496 times.
✓ Branch 2 taken 243 times.
✓ Branch 3 taken 7003 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 243 times.
160985 if (args->vararg && args->vararg->annotation &&
2441 243 !compiler_visit_argannotation(c, args->vararg->arg,
2442 243 args->vararg->annotation, &annotations_len))
2443 return 0;
2444
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 160742 times.
160742 if (!compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len))
2445 return 0;
2446
5/6
✓ Branch 0 taken 9906 times.
✓ Branch 1 taken 150836 times.
✓ Branch 2 taken 205 times.
✓ Branch 3 taken 9701 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 205 times.
160947 if (args->kwarg && args->kwarg->annotation &&
2447 205 !compiler_visit_argannotation(c, args->kwarg->arg,
2448 205 args->kwarg->annotation, &annotations_len))
2449 return 0;
2450
2451
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 160742 times.
160742 if (!compiler_visit_argannotation(c, &_Py_ID(return), returns,
2452 &annotations_len)) {
2453 return 0;
2454 }
2455
2456
2/2
✓ Branch 0 taken 13673 times.
✓ Branch 1 taken 147069 times.
160742 if (annotations_len) {
2457
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13673 times.
13673 ADDOP_I(c, BUILD_TUPLE, annotations_len);
2458 13673 return 1;
2459 }
2460
2461 147069 return -1;
2462 }
2463
2464 static int
2465 20527 compiler_visit_defaults(struct compiler *c, arguments_ty args)
2466 {
2467
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 53035 times.
✓ Branch 3 taken 73562 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 53035 times.
✓ Branch 6 taken 20527 times.
73562 VISIT_SEQ(c, expr, args->defaults);
2468
2/4
✓ Branch 0 taken 20527 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 20527 times.
20527 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2469 20527 return 1;
2470 }
2471
2472 static Py_ssize_t
2473 260906 compiler_default_arguments(struct compiler *c, arguments_ty args)
2474 {
2475 260906 Py_ssize_t funcflags = 0;
2476
4/6
✓ Branch 0 taken 260906 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 260906 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 20527 times.
✓ Branch 5 taken 240379 times.
260906 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
2477
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20527 times.
20527 if (!compiler_visit_defaults(c, args))
2478 return -1;
2479 20527 funcflags |= 0x01;
2480 }
2481
1/2
✓ Branch 0 taken 260906 times.
✗ Branch 1 not taken.
260906 if (args->kwonlyargs) {
2482 260906 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
2483 args->kw_defaults);
2484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 260906 times.
260906 if (res == 0) {
2485 return -1;
2486 }
2487
2/2
✓ Branch 0 taken 1022 times.
✓ Branch 1 taken 259884 times.
260906 else if (res > 0) {
2488 1022 funcflags |= 0x02;
2489 }
2490 }
2491 260906 return funcflags;
2492 }
2493
2494 static int
2495 6395660 forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2496 {
2497
2498
3/4
✓ Branch 0 taken 2073709 times.
✓ Branch 1 taken 4321951 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2073709 times.
6395660 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2499 compiler_error(c, "cannot assign to __debug__");
2500 return 1;
2501 }
2502
3/4
✓ Branch 0 taken 4894 times.
✓ Branch 1 taken 6390766 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4894 times.
6395660 if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2503 compiler_error(c, "cannot delete __debug__");
2504 return 1;
2505 }
2506 6395660 return 0;
2507 }
2508
2509 static int
2510 1462448 compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2511 {
2512
2/2
✓ Branch 0 taken 957925 times.
✓ Branch 1 taken 504523 times.
1462448 if (arg != NULL) {
2513
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 957925 times.
957925 if (forbidden_name(c, arg->arg, Store))
2514 return 0;
2515 }
2516 1462448 return 1;
2517 }
2518
2519 static int
2520 782718 compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
2521 {
2522
1/2
✓ Branch 0 taken 782718 times.
✗ Branch 1 not taken.
782718 if (args != NULL) {
2523
3/4
✓ Branch 0 taken 782718 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 940636 times.
✓ Branch 3 taken 782718 times.
1723354 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
2524
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 940636 times.
940636 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2525 return 0;
2526 }
2527 }
2528 782718 return 1;
2529 }
2530
2531 static int
2532 260906 compiler_check_debug_args(struct compiler *c, arguments_ty args)
2533 {
2534
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 260906 times.
260906 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2535 return 0;
2536
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 260906 times.
260906 if (!compiler_check_debug_args_seq(c, args->args))
2537 return 0;
2538
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 260906 times.
260906 if (!compiler_check_debug_one_arg(c, args->vararg))
2539 return 0;
2540
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 260906 times.
260906 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2541 return 0;
2542
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 260906 times.
260906 if (!compiler_check_debug_one_arg(c, args->kwarg))
2543 return 0;
2544 260906 return 1;
2545 }
2546
2547 static int
2548 160742 compiler_function(struct compiler *c, stmt_ty s, int is_async)
2549 {
2550 PyCodeObject *co;
2551 160742 PyObject *qualname, *docstring = NULL;
2552 arguments_ty args;
2553 expr_ty returns;
2554 identifier name;
2555 asdl_expr_seq* decos;
2556 asdl_stmt_seq *body;
2557 Py_ssize_t i, funcflags;
2558 int annotations;
2559 int scope_type;
2560 int firstlineno;
2561
2562
2/2
✓ Branch 0 taken 1106 times.
✓ Branch 1 taken 159636 times.
160742 if (is_async) {
2563 assert(s->kind == AsyncFunctionDef_kind);
2564
2565 1106 args = s->v.AsyncFunctionDef.args;
2566 1106 returns = s->v.AsyncFunctionDef.returns;
2567 1106 decos = s->v.AsyncFunctionDef.decorator_list;
2568 1106 name = s->v.AsyncFunctionDef.name;
2569 1106 body = s->v.AsyncFunctionDef.body;
2570
2571 1106 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2572 } else {
2573 assert(s->kind == FunctionDef_kind);
2574
2575 159636 args = s->v.FunctionDef.args;
2576 159636 returns = s->v.FunctionDef.returns;
2577 159636 decos = s->v.FunctionDef.decorator_list;
2578 159636 name = s->v.FunctionDef.name;
2579 159636 body = s->v.FunctionDef.body;
2580
2581 159636 scope_type = COMPILER_SCOPE_FUNCTION;
2582 }
2583
2584
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 160742 times.
160742 if (!compiler_check_debug_args(c, args))
2585 return 0;
2586
2587
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 160742 times.
160742 if (!compiler_decorators(c, decos))
2588 return 0;
2589
2590 160742 firstlineno = s->lineno;
2591
3/4
✓ Branch 0 taken 13563 times.
✓ Branch 1 taken 147179 times.
✓ Branch 2 taken 13563 times.
✗ Branch 3 not taken.
160742 if (asdl_seq_LEN(decos)) {
2592 13563 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2593 }
2594
2595 160742 funcflags = compiler_default_arguments(c, args);
2596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 160742 times.
160742 if (funcflags == -1) {
2597 return 0;
2598 }
2599
2600 160742 annotations = compiler_visit_annotations(c, args, returns);
2601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 160742 times.
160742 if (annotations == 0) {
2602 return 0;
2603 }
2604
2/2
✓ Branch 0 taken 13673 times.
✓ Branch 1 taken 147069 times.
160742 else if (annotations > 0) {
2605 13673 funcflags |= 0x04;
2606 }
2607
2608
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 160742 times.
160742 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
2609 return 0;
2610 }
2611
2612 /* if not -OO mode, add docstring */
2613
1/2
✓ Branch 0 taken 160742 times.
✗ Branch 1 not taken.
160742 if (c->c_optimize < 2) {
2614 160742 docstring = _PyAST_GetDocString(body);
2615 }
2616
3/4
✓ Branch 0 taken 29645 times.
✓ Branch 1 taken 131097 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 160742 times.
160742 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
2617 compiler_exit_scope(c);
2618 return 0;
2619 }
2620
2621
1/2
✓ Branch 0 taken 160742 times.
✗ Branch 1 not taken.
160742 c->u->u_argcount = asdl_seq_LEN(args->args);
2622
1/2
✓ Branch 0 taken 160742 times.
✗ Branch 1 not taken.
160742 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
2623
1/2
✓ Branch 0 taken 160742 times.
✗ Branch 1 not taken.
160742 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2624
3/4
✓ Branch 0 taken 706240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 545498 times.
✓ Branch 3 taken 160742 times.
706240 for (i = docstring ? 1 : 0; i < asdl_seq_LEN(body); i++) {
2625
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 545498 times.
545498 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
2626 }
2627 160742 co = assemble(c, 1);
2628 160742 qualname = c->u->u_qualname;
2629 160742 Py_INCREF(qualname);
2630 160742 compiler_exit_scope(c);
2631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 160742 times.
160742 if (co == NULL) {
2632 Py_XDECREF(qualname);
2633 Py_XDECREF(co);
2634 return 0;
2635 }
2636
2637
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 160742 times.
160742 if (!compiler_make_closure(c, co, funcflags, qualname)) {
2638 Py_DECREF(qualname);
2639 Py_DECREF(co);
2640 return 0;
2641 }
2642 160742 Py_DECREF(qualname);
2643 160742 Py_DECREF(co);
2644
2645
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 160742 times.
160742 if (!compiler_apply_decorators(c, decos))
2646 return 0;
2647 160742 return compiler_nameop(c, name, Store);
2648 }
2649
2650 static int
2651 15204 compiler_class(struct compiler *c, stmt_ty s)
2652 {
2653 PyCodeObject *co;
2654 int i, firstlineno;
2655 15204 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
2656
2657
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 if (!compiler_decorators(c, decos))
2658 return 0;
2659
2660 15204 firstlineno = s->lineno;
2661
3/4
✓ Branch 0 taken 487 times.
✓ Branch 1 taken 14717 times.
✓ Branch 2 taken 487 times.
✗ Branch 3 not taken.
15204 if (asdl_seq_LEN(decos)) {
2662 487 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2663 }
2664
2665 /* ultimately generate code for:
2666 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2667 where:
2668 <func> is a zero arg function/closure created from the class body.
2669 It mutates its locals to build the class namespace.
2670 <name> is the class name
2671 <bases> is the positional arguments and *varargs argument
2672 <keywords> is the keyword arguments and **kwds argument
2673 This borrows from compiler_call.
2674 */
2675
2676 /* 1. compile the class body into a code object */
2677
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 if (!compiler_enter_scope(c, s->v.ClassDef.name,
2678 COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) {
2679 return 0;
2680 }
2681 /* this block represents what we do in the new scope */
2682 {
2683 /* use the class name for name mangling */
2684 15204 Py_INCREF(s->v.ClassDef.name);
2685 15204 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
2686 /* load (global) __name__ ... */
2687
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 if (!compiler_nameop(c, &_Py_ID(__name__), Load)) {
2688 compiler_exit_scope(c);
2689 return 0;
2690 }
2691 /* ... and store it as __module__ */
2692
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 if (!compiler_nameop(c, &_Py_ID(__module__), Store)) {
2693 compiler_exit_scope(c);
2694 return 0;
2695 }
2696 assert(c->u->u_qualname);
2697
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 ADDOP_LOAD_CONST(c, c->u->u_qualname);
2698
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 if (!compiler_nameop(c, &_Py_ID(__qualname__), Store)) {
2699 compiler_exit_scope(c);
2700 return 0;
2701 }
2702 /* compile the body proper */
2703
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 if (!compiler_body(c, s->v.ClassDef.body)) {
2704 compiler_exit_scope(c);
2705 return 0;
2706 }
2707 /* The following code is artificial */
2708 15204 UNSET_LOC(c);
2709 /* Return __classcell__ if it is referenced, otherwise return None */
2710
2/2
✓ Branch 0 taken 2701 times.
✓ Branch 1 taken 12503 times.
15204 if (c->u->u_ste->ste_needs_class_closure) {
2711 /* Store __classcell__ into class namespace & return it */
2712 2701 i = compiler_lookup_arg(c->u->u_cellvars, &_Py_ID(__class__));
2713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2701 times.
2701 if (i < 0) {
2714 compiler_exit_scope(c);
2715 return 0;
2716 }
2717 assert(i == 0);
2718
2719
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2701 times.
2701 ADDOP_I(c, LOAD_CLOSURE, i);
2720
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2701 times.
2701 ADDOP_I(c, COPY, 1);
2721
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2701 times.
2701 if (!compiler_nameop(c, &_Py_ID(__classcell__), Store)) {
2722 compiler_exit_scope(c);
2723 return 0;
2724 }
2725 }
2726 else {
2727 /* No methods referenced __class__, so just return None */
2728 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
2729
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12503 times.
12503 ADDOP_LOAD_CONST(c, Py_None);
2730 }
2731
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 ADDOP_IN_SCOPE(c, RETURN_VALUE);
2732 /* create the code object */
2733 15204 co = assemble(c, 1);
2734 }
2735 /* leave the new scope */
2736 15204 compiler_exit_scope(c);
2737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15204 times.
15204 if (co == NULL)
2738 return 0;
2739
2740 /* 2. load the 'build_class' function */
2741
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 ADDOP(c, PUSH_NULL);
2742
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 ADDOP(c, LOAD_BUILD_CLASS);
2743
2744 /* 3. load a function (or closure) made from the code object */
2745
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 if (!compiler_make_closure(c, co, 0, NULL)) {
2746 Py_DECREF(co);
2747 return 0;
2748 }
2749 15204 Py_DECREF(co);
2750
2751 /* 4. load class name */
2752
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
2753
2754 /* 5. generate the rest of the code for the call */
2755
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
2756 return 0;
2757 /* 6. apply decorators */
2758
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 if (!compiler_apply_decorators(c, decos))
2759 return 0;
2760
2761 /* 7. store into <name> */
2762
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15204 times.
15204 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2763 return 0;
2764 15204 return 1;
2765 }
2766
2767 /* Return 0 if the expression is a constant value except named singletons.
2768 Return 1 otherwise. */
2769 static int
2770 344853 check_is_arg(expr_ty e)
2771 {
2772
2/2
✓ Branch 0 taken 257103 times.
✓ Branch 1 taken 87750 times.
344853 if (e->kind != Constant_kind) {
2773 257103 return 1;
2774 }
2775 87750 PyObject *value = e->v.Constant.value;
2776 return (value == Py_None
2777
2/2
✓ Branch 0 taken 63356 times.
✓ Branch 1 taken 3272 times.
66628 || value == Py_False
2778
2/2
✓ Branch 0 taken 60353 times.
✓ Branch 1 taken 3003 times.
63356 || value == Py_True
2779
4/4
✓ Branch 0 taken 66628 times.
✓ Branch 1 taken 21122 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 60339 times.
154378 || value == Py_Ellipsis);
2780 }
2781
2782 /* Check operands of identity chacks ("is" and "is not").
2783 Emit a warning if any operand is a constant except named singletons.
2784 Return 0 on error.
2785 */
2786 static int
2787 171329 check_compare(struct compiler *c, expr_ty e)
2788 {
2789 Py_ssize_t i, n;
2790 171329 int left = check_is_arg(e->v.Compare.left);
2791
1/2
✓ Branch 0 taken 171329 times.
✗ Branch 1 not taken.
171329 n = asdl_seq_LEN(e->v.Compare.ops);
2792
2/2
✓ Branch 0 taken 173524 times.
✓ Branch 1 taken 171329 times.
344853 for (i = 0; i < n; i++) {
2793 173524 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2794 173524 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2795
4/4
✓ Branch 0 taken 147797 times.
✓ Branch 1 taken 25727 times.
✓ Branch 2 taken 11709 times.
✓ Branch 3 taken 136088 times.
173524 if (op == Is || op == IsNot) {
2796
2/4
✓ Branch 0 taken 37436 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 37436 times.
37436 if (!right || !left) {
2797 const char *msg = (op == Is)
2798 ? "\"is\" with a literal. Did you mean \"==\"?"
2799 : "\"is not\" with a literal. Did you mean \"!=\"?";
2800 return compiler_warn(c, msg);
2801 }
2802 }
2803 173524 left = right;
2804 }
2805 171329 return 1;
2806 }
2807
2808 173525 static int compiler_addcompare(struct compiler *c, cmpop_ty op)
2809 {
2810 int cmp;
2811
10/11
✓ Branch 0 taken 86862 times.
✓ Branch 1 taken 9733 times.
✓ Branch 2 taken 9477 times.
✓ Branch 3 taken 4497 times.
✓ Branch 4 taken 6711 times.
✓ Branch 5 taken 3194 times.
✓ Branch 6 taken 25728 times.
✓ Branch 7 taken 11709 times.
✓ Branch 8 taken 11316 times.
✓ Branch 9 taken 4298 times.
✗ Branch 10 not taken.
173525 switch (op) {
2812 86862 case Eq:
2813 86862 cmp = Py_EQ;
2814 86862 break;
2815 9733 case NotEq:
2816 9733 cmp = Py_NE;
2817 9733 break;
2818 9477 case Lt:
2819 9477 cmp = Py_LT;
2820 9477 break;
2821 4497 case LtE:
2822 4497 cmp = Py_LE;
2823 4497 break;
2824 6711 case Gt:
2825 6711 cmp = Py_GT;
2826 6711 break;
2827 3194 case GtE:
2828 3194 cmp = Py_GE;
2829 3194 break;
2830 25728 case Is:
2831
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25728 times.
25728 ADDOP_I(c, IS_OP, 0);
2832 25728 return 1;
2833 11709 case IsNot:
2834
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11709 times.
11709 ADDOP_I(c, IS_OP, 1);
2835 11709 return 1;
2836 11316 case In:
2837
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11316 times.
11316 ADDOP_I(c, CONTAINS_OP, 0);
2838 11316 return 1;
2839 4298 case NotIn:
2840
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4298 times.
4298 ADDOP_I(c, CONTAINS_OP, 1);
2841 4298 return 1;
2842 default:
2843 Py_UNREACHABLE();
2844 }
2845
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 120474 times.
120474 ADDOP_I(c, COMPARE_OP, cmp);
2846 120474 return 1;
2847 }
2848
2849
2850
2851 static int
2852 294229 compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2853 {
2854
5/5
✓ Branch 0 taken 20533 times.
✓ Branch 1 taken 19747 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 150213 times.
✓ Branch 4 taken 103727 times.
294229 switch (e->kind) {
2855 20533 case UnaryOp_kind:
2856
2/2
✓ Branch 0 taken 20531 times.
✓ Branch 1 taken 2 times.
20533 if (e->v.UnaryOp.op == Not)
2857 20531 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2858 /* fallback to general implementation */
2859 2 break;
2860 19747 case BoolOp_kind: {
2861 19747 asdl_expr_seq *s = e->v.BoolOp.values;
2862
1/2
✓ Branch 0 taken 19747 times.
✗ Branch 1 not taken.
19747 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2863 assert(n >= 0);
2864 19747 int cond2 = e->v.BoolOp.op == Or;
2865 19747 basicblock *next2 = next;
2866
2/2
✓ Branch 0 taken 6689 times.
✓ Branch 1 taken 13058 times.
19747 if (!cond2 != !cond) {
2867 6689 next2 = compiler_new_block(c);
2868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6689 times.
6689 if (next2 == NULL)
2869 return 0;
2870 }
2871
2/2
✓ Branch 0 taken 22518 times.
✓ Branch 1 taken 19747 times.
42265 for (i = 0; i < n; ++i) {
2872
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22518 times.
22518 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2873 return 0;
2874 }
2875
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19747 times.
19747 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2876 return 0;
2877
2/2
✓ Branch 0 taken 6689 times.
✓ Branch 1 taken 13058 times.
19747 if (next2 != next)
2878 6689 compiler_use_next_block(c, next2);
2879 19747 return 1;
2880 }
2881 9 case IfExp_kind: {
2882 basicblock *end, *next2;
2883 9 end = compiler_new_block(c);
2884
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (end == NULL)
2885 return 0;
2886 9 next2 = compiler_new_block(c);
2887
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (next2 == NULL)
2888 return 0;
2889
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2890 return 0;
2891
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2892 return 0;
2893
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 ADDOP_JUMP_NOLINE(c, JUMP, end);
2894 9 compiler_use_next_block(c, next2);
2895
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2896 return 0;
2897 9 compiler_use_next_block(c, end);
2898 9 return 1;
2899 }
2900 150213 case Compare_kind: {
2901
1/2
✓ Branch 0 taken 150213 times.
✗ Branch 1 not taken.
150213 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2902
2/2
✓ Branch 0 taken 1961 times.
✓ Branch 1 taken 148252 times.
150213 if (n > 0) {
2903
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1961 times.
1961 if (!check_compare(c, e)) {
2904 return 0;
2905 }
2906 1961 basicblock *cleanup = compiler_new_block(c);
2907
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1961 times.
1961 if (cleanup == NULL)
2908 return 0;
2909
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1961 times.
1961 VISIT(c, expr, e->v.Compare.left);
2910
2/2
✓ Branch 0 taken 2123 times.
✓ Branch 1 taken 1961 times.
4084 for (i = 0; i < n; i++) {
2911
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2123 times.
2123 VISIT(c, expr,
2912 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2913
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2123 times.
2123 ADDOP_I(c, SWAP, 2);
2914
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2123 times.
2123 ADDOP_I(c, COPY, 2);
2915
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2123 times.
2123 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
2916
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2123 times.
2123 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
2917 }
2918
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1961 times.
1961 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
2919
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1961 times.
1961 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
2920
3/4
✓ Branch 0 taken 1017 times.
✓ Branch 1 taken 944 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1961 times.
1961 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2921 1961 basicblock *end = compiler_new_block(c);
2922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1961 times.
1961 if (end == NULL)
2923 return 0;
2924
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1961 times.
1961 ADDOP_JUMP_NOLINE(c, JUMP, end);
2925 1961 compiler_use_next_block(c, cleanup);
2926
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1961 times.
1961 ADDOP(c, POP_TOP);
2927
2/2
✓ Branch 0 taken 944 times.
✓ Branch 1 taken 1017 times.
1961 if (!cond) {
2928
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 944 times.
944 ADDOP_JUMP_NOLINE(c, JUMP, next);
2929 }
2930 1961 compiler_use_next_block(c, end);
2931 1961 return 1;
2932 }
2933 /* fallback to general implementation */
2934 148252 break;
2935 }
2936 103727 default:
2937 /* fallback to general implementation */
2938 103727 break;
2939 }
2940
2941 /* general implementation */
2942
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 251981 times.
251981 VISIT(c, expr, e);
2943
3/4
✓ Branch 0 taken 103727 times.
✓ Branch 1 taken 148254 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 251981 times.
251981 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2944 251981 return 1;
2945 }
2946
2947 static int
2948 5964 compiler_ifexp(struct compiler *c, expr_ty e)
2949 {
2950 basicblock *end, *next;
2951
2952 assert(e->kind == IfExp_kind);
2953 5964 end = compiler_new_block(c);
2954
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5964 times.
5964 if (end == NULL)
2955 return 0;
2956 5964 next = compiler_new_block(c);
2957
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5964 times.
5964 if (next == NULL)
2958 return 0;
2959
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5964 times.
5964 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2960 return 0;
2961
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5964 times.
5964 VISIT(c, expr, e->v.IfExp.body);
2962
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5964 times.
5964 ADDOP_JUMP_NOLINE(c, JUMP, end);
2963 5964 compiler_use_next_block(c, next);
2964
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5964 times.
5964 VISIT(c, expr, e->v.IfExp.orelse);
2965 5964 compiler_use_next_block(c, end);
2966 5964 return 1;
2967 }
2968
2969 static int
2970 100164 compiler_lambda(struct compiler *c, expr_ty e)
2971 {
2972 PyCodeObject *co;
2973 PyObject *qualname;
2974 Py_ssize_t funcflags;
2975 100164 arguments_ty args = e->v.Lambda.args;
2976 assert(e->kind == Lambda_kind);
2977
2978
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 100164 times.
100164 if (!compiler_check_debug_args(c, args))
2979 return 0;
2980
2981 100164 funcflags = compiler_default_arguments(c, args);
2982
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100164 times.
100164 if (funcflags == -1) {
2983 return 0;
2984 }
2985
2986 _Py_DECLARE_STR(anon_lambda, "<lambda>");
2987
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 100164 times.
100164 if (!compiler_enter_scope(c, &_Py_STR(anon_lambda), COMPILER_SCOPE_LAMBDA,
2988 (void *)e, e->lineno)) {
2989 return 0;
2990 }
2991 /* Make None the first constant, so the lambda can't have a
2992 docstring. */
2993
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 100164 times.
100164 if (compiler_add_const(c, Py_None) < 0)
2994 return 0;
2995
2996
1/2
✓ Branch 0 taken 100164 times.
✗ Branch 1 not taken.
100164 c->u->u_argcount = asdl_seq_LEN(args->args);
2997
1/2
✓ Branch 0 taken 100164 times.
✗ Branch 1 not taken.
100164 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
2998
1/2
✓ Branch 0 taken 100164 times.
✗ Branch 1 not taken.
100164 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2999
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 100164 times.
100164 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
3000
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 100160 times.
100164 if (c->u->u_ste->ste_generator) {
3001 4 co = assemble(c, 0);
3002 }
3003 else {
3004
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 100160 times.
100160 ADDOP_IN_SCOPE(c, RETURN_VALUE);
3005 100160 co = assemble(c, 1);
3006 }
3007 100164 qualname = c->u->u_qualname;
3008 100164 Py_INCREF(qualname);
3009 100164 compiler_exit_scope(c);
3010
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100164 times.
100164 if (co == NULL) {
3011 Py_DECREF(qualname);
3012 return 0;
3013 }
3014
3015
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 100164 times.
100164 if (!compiler_make_closure(c, co, funcflags, qualname)) {
3016 Py_DECREF(qualname);
3017 Py_DECREF(co);
3018 return 0;
3019 }
3020 100164 Py_DECREF(qualname);
3021 100164 Py_DECREF(co);
3022
3023 100164 return 1;
3024 }
3025
3026 static int
3027 137065 compiler_if(struct compiler *c, stmt_ty s)
3028 {
3029 basicblock *end, *next;
3030 assert(s->kind == If_kind);
3031 137065 end = compiler_new_block(c);
3032
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 137065 times.
137065 if (end == NULL) {
3033 return 0;
3034 }
3035
3/4
✓ Branch 0 taken 41953 times.
✓ Branch 1 taken 95112 times.
✓ Branch 2 taken 41953 times.
✗ Branch 3 not taken.
137065 if (asdl_seq_LEN(s->v.If.orelse)) {
3036 41953 next = compiler_new_block(c);
3037
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41953 times.
41953 if (next == NULL) {
3038 return 0;
3039 }
3040 }
3041 else {
3042 95112 next = end;
3043 }
3044
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 137065 times.
137065 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
3045 return 0;
3046 }
3047
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 223134 times.
✓ Branch 3 taken 360199 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 223134 times.
✓ Branch 6 taken 137065 times.
360199 VISIT_SEQ(c, stmt, s->v.If.body);
3048
3/4
✓ Branch 0 taken 41953 times.
✓ Branch 1 taken 95112 times.
✓ Branch 2 taken 41953 times.
✗ Branch 3 not taken.
137065 if (asdl_seq_LEN(s->v.If.orelse)) {
3049
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 41953 times.
41953 ADDOP_JUMP_NOLINE(c, JUMP, end);
3050 41953 compiler_use_next_block(c, next);
3051
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 54901 times.
✓ Branch 3 taken 96854 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 54901 times.
✓ Branch 6 taken 41953 times.
96854 VISIT_SEQ(c, stmt, s->v.If.orelse);
3052 }
3053 137065 compiler_use_next_block(c, end);
3054 137065 return 1;
3055 }
3056
3057 static int
3058 33890 compiler_for(struct compiler *c, stmt_ty s)
3059 {
3060 basicblock *start, *body, *cleanup, *end;
3061
3062 33890 start = compiler_new_block(c);
3063 33890 body = compiler_new_block(c);
3064 33890 cleanup = compiler_new_block(c);
3065 33890 end = compiler_new_block(c);
3066
4/8
✓ Branch 0 taken 33890 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33890 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33890 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 33890 times.
33890 if (start == NULL || body == NULL || end == NULL || cleanup == NULL) {
3067 return 0;
3068 }
3069
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33890 times.
33890 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
3070 return 0;
3071 }
3072
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33890 times.
33890 VISIT(c, expr, s->v.For.iter);
3073
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33890 times.
33890 ADDOP(c, GET_ITER);
3074 33890 compiler_use_next_block(c, start);
3075
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33890 times.
33890 ADDOP_JUMP(c, FOR_ITER, cleanup);
3076 33890 compiler_use_next_block(c, body);
3077
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33890 times.
33890 VISIT(c, expr, s->v.For.target);
3078
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 121639 times.
✓ Branch 3 taken 155529 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 121639 times.
✓ Branch 6 taken 33890 times.
155529 VISIT_SEQ(c, stmt, s->v.For.body);
3079 /* Mark jump as artificial */
3080 33890 UNSET_LOC(c);
3081
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33890 times.
33890 ADDOP_JUMP(c, JUMP, start);
3082 33890 compiler_use_next_block(c, cleanup);
3083
3084 33890 compiler_pop_fblock(c, FOR_LOOP, start);
3085
3086
5/6
✗ Branch 1 not taken.
✓ Branch 2 taken 659 times.
✓ Branch 3 taken 1158 times.
✓ Branch 4 taken 33391 times.
✓ Branch 5 taken 659 times.
✓ Branch 6 taken 33890 times.
34549 VISIT_SEQ(c, stmt, s->v.For.orelse);
3087 33890 compiler_use_next_block(c, end);
3088 33890 return 1;
3089 }
3090
3091
3092 static int
3093 3 compiler_async_for(struct compiler *c, stmt_ty s)
3094 {
3095 basicblock *start, *except, *end;
3096
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
3 if (IS_TOP_LEVEL_AWAIT(c)){
3097 c->u->u_ste->ste_coroutine = 1;
3098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
3099 return compiler_error(c, "'async for' outside async function");
3100 }
3101
3102 3 start = compiler_new_block(c);
3103 3 except = compiler_new_block(c);
3104 3 end = compiler_new_block(c);
3105
3106
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
3 if (start == NULL || except == NULL || end == NULL) {
3107 return 0;
3108 }
3109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 VISIT(c, expr, s->v.AsyncFor.iter);
3110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 ADDOP(c, GET_AITER);
3111
3112 3 compiler_use_next_block(c, start);
3113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
3114 return 0;
3115 }
3116 /* SETUP_FINALLY to guard the __anext__ call */
3117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 ADDOP_JUMP(c, SETUP_FINALLY, except);
3118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 ADDOP(c, GET_ANEXT);
3119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 ADDOP_LOAD_CONST(c, Py_None);
3120
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 ADD_YIELD_FROM(c, 1);
3121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
3122
3123 /* Success block for __anext__ */
3124
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 VISIT(c, expr, s->v.AsyncFor.target);
3125
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 3 times.
9 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
3126 /* Mark jump as artificial */
3127 3 UNSET_LOC(c);
3128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 ADDOP_JUMP(c, JUMP, start);
3129
3130 3 compiler_pop_fblock(c, FOR_LOOP, start);
3131
3132 /* Except block for __anext__ */
3133 3 compiler_use_next_block(c, except);
3134
3135 /* Use same line number as the iterator,
3136 * as the END_ASYNC_FOR succeeds the `for`, not the body. */
3137 3 SET_LOC(c, s->v.AsyncFor.iter);
3138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 ADDOP(c, END_ASYNC_FOR);
3139
3140 /* `else` block */
3141
2/6
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
3 VISIT_SEQ(c, stmt, s->v.For.orelse);
3142
3143 3 compiler_use_next_block(c, end);
3144
3145 3 return 1;
3146 }
3147
3148 static int
3149 4100 compiler_while(struct compiler *c, stmt_ty s)
3150 {
3151 4100 basicblock *loop, *body, *end, *anchor = NULL;
3152 4100 loop = compiler_new_block(c);
3153 4100 body = compiler_new_block(c);
3154 4100 anchor = compiler_new_block(c);
3155 4100 end = compiler_new_block(c);
3156
4/8
✓ Branch 0 taken 4100 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4100 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4100 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4100 times.
4100 if (loop == NULL || body == NULL || anchor == NULL || end == NULL) {
3157 return 0;
3158 }
3159 4100 compiler_use_next_block(c, loop);
3160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4100 times.
4100 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) {
3161 return 0;
3162 }
3163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4100 times.
4100 if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) {
3164 return 0;
3165 }
3166
3167 4100 compiler_use_next_block(c, body);
3168
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 14035 times.
✓ Branch 3 taken 18135 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 14035 times.
✓ Branch 6 taken 4100 times.
18135 VISIT_SEQ(c, stmt, s->v.While.body);
3169 4100 SET_LOC(c, s);
3170
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4100 times.
4100 if (!compiler_jump_if(c, s->v.While.test, body, 1)) {
3171 return 0;
3172 }
3173
3174 4100 compiler_pop_fblock(c, WHILE_LOOP, loop);
3175
3176 4100 compiler_use_next_block(c, anchor);
3177
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 4047 times.
4100 if (s->v.While.orelse) {
3178
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 71 times.
✓ Branch 3 taken 124 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 71 times.
✓ Branch 6 taken 53 times.
124 VISIT_SEQ(c, stmt, s->v.While.orelse);
3179 }
3180 4100 compiler_use_next_block(c, end);
3181
3182 4100 return 1;
3183 }
3184
3185 static int
3186 150682 compiler_return(struct compiler *c, stmt_ty s)
3187 {
3188
2/2
✓ Branch 0 taken 147620 times.
✓ Branch 1 taken 3062 times.
298302 int preserve_tos = ((s->v.Return.value != NULL) &&
3189
2/2
✓ Branch 0 taken 133916 times.
✓ Branch 1 taken 13704 times.
147620 (s->v.Return.value->kind != Constant_kind));
3190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 150682 times.
150682 if (c->u->u_ste->ste_type != FunctionBlock)
3191 return compiler_error(c, "'return' outside function");
3192
2/2
✓ Branch 0 taken 147620 times.
✓ Branch 1 taken 3062 times.
150682 if (s->v.Return.value != NULL &&
3193
3/4
✓ Branch 0 taken 644 times.
✓ Branch 1 taken 146976 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 644 times.
147620 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
3194 {
3195 return compiler_error(
3196 c, "'return' with value in async generator");
3197 }
3198
2/2
✓ Branch 0 taken 133916 times.
✓ Branch 1 taken 16766 times.
150682 if (preserve_tos) {
3199
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 133916 times.
133916 VISIT(c, expr, s->v.Return.value);
3200 } else {
3201 /* Emit instruction with line number for return value */
3202
2/2
✓ Branch 0 taken 13704 times.
✓ Branch 1 taken 3062 times.
16766 if (s->v.Return.value != NULL) {
3203 13704 SET_LOC(c, s->v.Return.value);
3204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13704 times.
13704 ADDOP(c, NOP);
3205 }
3206 }
3207
4/4
✓ Branch 0 taken 147620 times.
✓ Branch 1 taken 3062 times.
✓ Branch 2 taken 331 times.
✓ Branch 3 taken 147289 times.
150682 if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) {
3208 3393 SET_LOC(c, s);
3209
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3393 times.
3393 ADDOP(c, NOP);
3210 }
3211
3212
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 150682 times.
150682 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
3213 return 0;
3214
2/2
✓ Branch 0 taken 3062 times.
✓ Branch 1 taken 147620 times.
150682 if (s->v.Return.value == NULL) {
3215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3062 times.
3062 ADDOP_LOAD_CONST(c, Py_None);
3216 }
3217
2/2
✓ Branch 0 taken 13704 times.
✓ Branch 1 taken 133916 times.
147620 else if (!preserve_tos) {
3218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13704 times.
13704 ADDOP_LOAD_CONST(c, s->v.Return.value->v.Constant.value);
3219 }
3220
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 150682 times.
150682 ADDOP(c, RETURN_VALUE);
3221
3222 150682 return 1;
3223 }
3224
3225 static int
3226 2764 compiler_break(struct compiler *c)
3227 {
3228 2764 struct fblockinfo *loop = NULL;
3229 /* Emit instruction with line number */
3230
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
2764 ADDOP(c, NOP);
3231
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
2764 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3232 return 0;
3233 }
3234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2764 times.
2764 if (loop == NULL) {
3235 return compiler_error(c, "'break' outside loop");
3236 }
3237
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
2764 if (!compiler_unwind_fblock(c, loop, 0)) {
3238 return 0;
3239 }
3240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2764 times.
2764 ADDOP_JUMP(c, JUMP, loop->fb_exit);
3241 2764 return 1;
3242 }
3243
3244 static int
3245 2799 compiler_continue(struct compiler *c)
3246 {
3247 2799 struct fblockinfo *loop = NULL;
3248 /* Emit instruction with line number */
3249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2799 times.
2799 ADDOP(c, NOP);
3250
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2799 times.
2799 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
3251 return 0;
3252 }
3253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2799 times.
2799 if (loop == NULL) {
3254 return compiler_error(c, "'continue' not properly in loop");
3255 }
3256
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2799 times.
2799 ADDOP_JUMP(c, JUMP, loop->fb_block);
3257 2799 return 1;
3258 }
3259
3260
3261 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
3262
3263 SETUP_FINALLY L
3264 <code for body>
3265 POP_BLOCK
3266 <code for finalbody>
3267 JUMP E
3268 L:
3269 <code for finalbody>
3270 E:
3271
3272 The special instructions use the block stack. Each block
3273 stack entry contains the instruction that created it (here
3274 SETUP_FINALLY), the level of the value stack at the time the
3275 block stack entry was created, and a label (here L).
3276
3277 SETUP_FINALLY:
3278 Pushes the current value stack level and the label
3279 onto the block stack.
3280 POP_BLOCK:
3281 Pops en entry from the block stack.
3282
3283 The block stack is unwound when an exception is raised:
3284 when a SETUP_FINALLY entry is found, the raised and the caught
3285 exceptions are pushed onto the value stack (and the exception
3286 condition is cleared), and the interpreter jumps to the label
3287 gotten from the block stack.
3288 */
3289
3290 static int
3291 2032 compiler_try_finally(struct compiler *c, stmt_ty s)
3292 {
3293 basicblock *body, *end, *exit, *cleanup;
3294
3295 2032 body = compiler_new_block(c);
3296 2032 end = compiler_new_block(c);
3297 2032 exit = compiler_new_block(c);
3298 2032 cleanup = compiler_new_block(c);
3299
4/8
✓ Branch 0 taken 2032 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2032 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2032 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 2032 times.
2032 if (body == NULL || end == NULL || exit == NULL || cleanup == NULL) {
3300 return 0;
3301 }
3302 /* `try` block */
3303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2032 times.
2032 ADDOP_JUMP(c, SETUP_FINALLY, end);
3304 2032 compiler_use_next_block(c, body);
3305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2032 times.
2032 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
3306 return 0;
3307
4/6
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 1888 times.
✓ Branch 2 taken 144 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 144 times.
✗ Branch 5 not taken.
2032 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3308
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 144 times.
144 if (!compiler_try_except(c, s))
3309 return 0;
3310 }
3311 else {
3312
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 5484 times.
✓ Branch 3 taken 7372 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 5484 times.
✓ Branch 6 taken 1888 times.
7372 VISIT_SEQ(c, stmt, s->v.Try.body);
3313 }
3314
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2032 times.
2032 ADDOP_NOLINE(c, POP_BLOCK);
3315 2032 compiler_pop_fblock(c, FINALLY_TRY, body);
3316
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2439 times.
✓ Branch 3 taken 4471 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2439 times.
✓ Branch 6 taken 2032 times.
4471 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3317
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2032 times.
2032 ADDOP_JUMP_NOLINE(c, JUMP, exit);
3318 /* `finally` block */
3319 2032 compiler_use_next_block(c, end);
3320
3321 2032 UNSET_LOC(c);
3322
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2032 times.
2032 ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
3323
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2032 times.
2032 ADDOP(c, PUSH_EXC_INFO);
3324
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2032 times.
2032 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3325 return 0;
3326
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2439 times.
✓ Branch 3 taken 4471 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2439 times.
✓ Branch 6 taken 2032 times.
4471 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3327 2032 compiler_pop_fblock(c, FINALLY_END, end);
3328
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2032 times.
2032 ADDOP_I(c, RERAISE, 0);
3329 2032 compiler_use_next_block(c, cleanup);
3330
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2032 times.
2032 POP_EXCEPT_AND_RERAISE(c);
3331 2032 compiler_use_next_block(c, exit);
3332 2032 return 1;
3333 }
3334
3335 static int
3336 compiler_try_star_finally(struct compiler *c, stmt_ty s)
3337 {
3338 basicblock *body = compiler_new_block(c);
3339 if (body == NULL) {
3340 return 0;
3341 }
3342 basicblock *end = compiler_new_block(c);
3343 if (!end) {
3344 return 0;
3345 }
3346 basicblock *exit = compiler_new_block(c);
3347 if (!exit) {
3348 return 0;
3349 }
3350 basicblock *cleanup = compiler_new_block(c);
3351 if (!cleanup) {
3352 return 0;
3353 }
3354 /* `try` block */
3355 ADDOP_JUMP(c, SETUP_FINALLY, end);
3356 compiler_use_next_block(c, body);
3357 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.TryStar.finalbody)) {
3358 return 0;
3359 }
3360 if (s->v.TryStar.handlers && asdl_seq_LEN(s->v.TryStar.handlers)) {
3361 if (!compiler_try_star_except(c, s)) {
3362 return 0;
3363 }
3364 }
3365 else {
3366 VISIT_SEQ(c, stmt, s->v.TryStar.body);
3367 }
3368 ADDOP_NOLINE(c, POP_BLOCK);
3369 compiler_pop_fblock(c, FINALLY_TRY, body);
3370 VISIT_SEQ(c, stmt, s->v.TryStar.finalbody);
3371 ADDOP_JUMP_NOLINE(c, JUMP, exit);
3372 /* `finally` block */
3373 compiler_use_next_block(c, end);
3374
3375 UNSET_LOC(c);
3376 ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
3377 ADDOP(c, PUSH_EXC_INFO);
3378 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) {
3379 return 0;
3380 }
3381 VISIT_SEQ(c, stmt, s->v.TryStar.finalbody);
3382 compiler_pop_fblock(c, FINALLY_END, end);
3383 ADDOP_I(c, RERAISE, 0);
3384 compiler_use_next_block(c, cleanup);
3385 POP_EXCEPT_AND_RERAISE(c);
3386 compiler_use_next_block(c, exit);
3387 return 1;
3388 }
3389
3390
3391 /*
3392 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
3393 (The contents of the value stack is shown in [], with the top
3394 at the right; 'tb' is trace-back info, 'val' the exception's
3395 associated value, and 'exc' the exception.)
3396
3397 Value stack Label Instruction Argument
3398 [] SETUP_FINALLY L1
3399 [] <code for S>
3400 [] POP_BLOCK
3401 [] JUMP L0
3402
3403 [exc] L1: <evaluate E1> )
3404 [exc, E1] CHECK_EXC_MATCH )
3405 [exc, bool] POP_JUMP_IF_FALSE L2 ) only if E1
3406 [exc] <assign to V1> (or POP if no V1)
3407 [] <code for S1>
3408 JUMP L0
3409
3410 [exc] L2: <evaluate E2>
3411 .............................etc.......................
3412
3413 [exc] Ln+1: RERAISE # re-raise exception
3414
3415 [] L0: <next statement>
3416
3417 Of course, parts are not generated if Vi or Ei is not present.
3418 */
3419 static int
3420 11333 compiler_try_except(struct compiler *c, stmt_ty s)
3421 {
3422 basicblock *body, *except, *end, *cleanup;
3423 Py_ssize_t i, n;
3424
3425 11333 body = compiler_new_block(c);
3426 11333 except = compiler_new_block(c);
3427 11333 end = compiler_new_block(c);
3428 11333 cleanup = compiler_new_block(c);
3429
4/8
✓ Branch 0 taken 11333 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11333 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11333 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 11333 times.
11333 if (body == NULL || except == NULL || end == NULL || cleanup == NULL)
3430 return 0;
3431
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11333 times.
11333 ADDOP_JUMP(c, SETUP_FINALLY, except);
3432 11333 compiler_use_next_block(c, body);
3433
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11333 times.
11333 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
3434 return 0;
3435
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 16577 times.
✓ Branch 3 taken 27910 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 16577 times.
✓ Branch 6 taken 11333 times.
27910 VISIT_SEQ(c, stmt, s->v.Try.body);
3436 11333 compiler_pop_fblock(c, TRY_EXCEPT, body);
3437
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11333 times.
11333 ADDOP_NOLINE(c, POP_BLOCK);
3438
4/6
✓ Branch 0 taken 1234 times.
✓ Branch 1 taken 10099 times.
✓ Branch 2 taken 1234 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1234 times.
✗ Branch 5 not taken.
11333 if (s->v.Try.orelse && asdl_seq_LEN(s->v.Try.orelse)) {
3439
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 1984 times.
✓ Branch 3 taken 3218 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1984 times.
✓ Branch 6 taken 1234 times.
3218 VISIT_SEQ(c, stmt, s->v.Try.orelse);
3440 }
3441
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11333 times.
11333 ADDOP_JUMP_NOLINE(c, JUMP, end);
3442
1/2
✓ Branch 0 taken 11333 times.
✗ Branch 1 not taken.
11333 n = asdl_seq_LEN(s->v.Try.handlers);
3443 11333 compiler_use_next_block(c, except);
3444
3445 11333 UNSET_LOC(c);
3446
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11333 times.
11333 ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
3447
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11333 times.
11333 ADDOP(c, PUSH_EXC_INFO);
3448 /* Runtime will push a block here, so we need to account for that */
3449
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11333 times.
11333 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3450 return 0;
3451
2/2
✓ Branch 0 taken 11938 times.
✓ Branch 1 taken 11333 times.
23271 for (i = 0; i < n; i++) {
3452 11938 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
3453 s->v.Try.handlers, i);
3454 11938 SET_LOC(c, handler);
3455
3/4
✓ Branch 0 taken 252 times.
✓ Branch 1 taken 11686 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 252 times.
11938 if (!handler->v.ExceptHandler.type && i < n-1) {
3456 return compiler_error(c, "default 'except:' must be last");
3457 }
3458 11938 except = compiler_new_block(c);
3459
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11938 times.
11938 if (except == NULL)
3460 return 0;
3461
2/2
✓ Branch 0 taken 11686 times.
✓ Branch 1 taken 252 times.
11938 if (handler->v.ExceptHandler.type) {
3462
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11686 times.
11686 VISIT(c, expr, handler->v.ExceptHandler.type);
3463
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11686 times.
11686 ADDOP(c, CHECK_EXC_MATCH);
3464
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11686 times.
11686 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, except);
3465 }
3466
2/2
✓ Branch 0 taken 2030 times.
✓ Branch 1 taken 9908 times.
11938 if (handler->v.ExceptHandler.name) {
3467 basicblock *cleanup_end, *cleanup_body;
3468
3469 2030 cleanup_end = compiler_new_block(c);
3470 2030 cleanup_body = compiler_new_block(c);
3471
2/4
✓ Branch 0 taken 2030 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2030 times.
2030 if (cleanup_end == NULL || cleanup_body == NULL) {
3472 return 0;
3473 }
3474
3475 2030 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3476
3477 /*
3478 try:
3479 # body
3480 except type as name:
3481 try:
3482 # body
3483 finally:
3484 name = None # in case body contains "del name"
3485 del name
3486 */
3487
3488 /* second try: */
3489
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2030 times.
2030 ADDOP_JUMP(c, SETUP_CLEANUP, cleanup_end);
3490 2030 compiler_use_next_block(c, cleanup_body);
3491
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2030 times.
2030 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
3492 return 0;
3493
3494 /* second # body */
3495
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 3291 times.
✓ Branch 3 taken 5321 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 3291 times.
✓ Branch 6 taken 2030 times.
5321 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
3496 2030 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3497 /* name = None; del name; # Mark as artificial */
3498 2030 UNSET_LOC(c);
3499
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2030 times.
2030 ADDOP(c, POP_BLOCK);
3500
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2030 times.
2030 ADDOP(c, POP_BLOCK);
3501
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2030 times.
2030 ADDOP(c, POP_EXCEPT);
3502
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2030 times.
2030 ADDOP_LOAD_CONST(c, Py_None);
3503 2030 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3504 2030 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
3505
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2030 times.
2030 ADDOP_JUMP(c, JUMP, end);
3506
3507 /* except: */
3508 2030 compiler_use_next_block(c, cleanup_end);
3509
3510 /* name = None; del name; # Mark as artificial */
3511 2030 UNSET_LOC(c);
3512
3513
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2030 times.
2030 ADDOP_LOAD_CONST(c, Py_None);
3514 2030 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3515 2030 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
3516
3517
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2030 times.
2030 ADDOP_I(c, RERAISE, 1);
3518 }
3519 else {
3520 basicblock *cleanup_body;
3521
3522 9908 cleanup_body = compiler_new_block(c);
3523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9908 times.
9908 if (!cleanup_body)
3524 return 0;
3525
3526
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9908 times.
9908 ADDOP(c, POP_TOP); /* exc_value */
3527 9908 compiler_use_next_block(c, cleanup_body);
3528
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9908 times.
9908 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
3529 return 0;
3530
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 12975 times.
✓ Branch 3 taken 22883 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12975 times.
✓ Branch 6 taken 9908 times.
22883 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
3531 9908 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3532 9908 UNSET_LOC(c);
3533
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9908 times.
9908 ADDOP(c, POP_BLOCK);
3534
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9908 times.
9908 ADDOP(c, POP_EXCEPT);
3535
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9908 times.
9908 ADDOP_JUMP(c, JUMP, end);
3536 }
3537 11938 compiler_use_next_block(c, except);
3538 }
3539 /* Mark as artificial */
3540 11333 UNSET_LOC(c);
3541 11333 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
3542
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11333 times.
11333 ADDOP_I(c, RERAISE, 0);
3543 11333 compiler_use_next_block(c, cleanup);
3544
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11333 times.
11333 POP_EXCEPT_AND_RERAISE(c);
3545 11333 compiler_use_next_block(c, end);
3546 11333 return 1;
3547 }
3548
3549 /*
3550 Code generated for "try: S except* E1 as V1: S1 except* E2 as V2: S2 ...":
3551 (The contents of the value stack is shown in [], with the top
3552 at the right; 'tb' is trace-back info, 'val' the exception instance,
3553 and 'typ' the exception's type.)
3554
3555 Value stack Label Instruction Argument
3556 [] SETUP_FINALLY L1
3557 [] <code for S>
3558 [] POP_BLOCK
3559 [] JUMP L0
3560
3561 [exc] L1: COPY 1 ) save copy of the original exception
3562 [orig, exc] BUILD_LIST ) list for raised/reraised excs ("result")
3563 [orig, exc, res] SWAP 2
3564
3565 [orig, res, exc] <evaluate E1>
3566 [orig, res, exc, E1] CHECK_EG_MATCH
3567 [orig, red, rest/exc, match?] COPY 1
3568 [orig, red, rest/exc, match?, match?] POP_JUMP_IF_NOT_NONE H1
3569 [orig, red, exc, None] POP_TOP
3570 [orig, red, exc] JUMP L2
3571
3572 [orig, res, rest, match] H1: <assign to V1> (or POP if no V1)
3573
3574 [orig, res, rest] SETUP_FINALLY R1
3575 [orig, res, rest] <code for S1>
3576 [orig, res, rest] JUMP L2
3577
3578 [orig, res, rest, i, v] R1: LIST_APPEND 3 ) exc raised in except* body - add to res
3579 [orig, res, rest, i] POP
3580
3581 [orig, res, rest] L2: <evaluate E2>
3582 .............................etc.......................
3583
3584 [orig, res, rest] Ln+1: LIST_APPEND 1 ) add unhandled exc to res (could be None)
3585
3586 [orig, res] PREP_RERAISE_STAR
3587 [exc] COPY 1
3588 [exc, exc] POP_JUMP_IF_NOT_NONE RER
3589 [exc] POP_TOP
3590 [] JUMP L0
3591
3592 [exc] RER: SWAP 2
3593 [exc, prev_exc_info] POP_EXCEPT
3594 [exc] RERAISE 0
3595
3596 [] L0: <next statement>
3597 */
3598 static int
3599 compiler_try_star_except(struct compiler *c, stmt_ty s)
3600 {
3601 basicblock *body = compiler_new_block(c);
3602 if (body == NULL) {
3603 return 0;
3604 }
3605 basicblock *except = compiler_new_block(c);
3606 if (except == NULL) {
3607 return 0;
3608 }
3609 basicblock *orelse = compiler_new_block(c);
3610 if (orelse == NULL) {
3611 return 0;
3612 }
3613 basicblock *end = compiler_new_block(c);
3614 if (end == NULL) {
3615 return 0;
3616 }
3617 basicblock *cleanup = compiler_new_block(c);
3618 if (cleanup == NULL) {
3619 return 0;
3620 }
3621 basicblock *reraise_star = compiler_new_block(c);
3622 if (reraise_star == NULL) {
3623 return 0;
3624 }
3625
3626 ADDOP_JUMP(c, SETUP_FINALLY, except);
3627 compiler_use_next_block(c, body);
3628 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL)) {
3629 return 0;
3630 }
3631 VISIT_SEQ(c, stmt, s->v.TryStar.body);
3632 compiler_pop_fblock(c, TRY_EXCEPT, body);
3633 ADDOP_NOLINE(c, POP_BLOCK);
3634 ADDOP_JUMP_NOLINE(c, JUMP, orelse);
3635 Py_ssize_t n = asdl_seq_LEN(s->v.TryStar.handlers);
3636 compiler_use_next_block(c, except);
3637
3638 UNSET_LOC(c);
3639 ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
3640 ADDOP(c, PUSH_EXC_INFO);
3641 /* Runtime will push a block here, so we need to account for that */
3642 if (!compiler_push_fblock(c, EXCEPTION_GROUP_HANDLER,
3643 NULL, NULL, "except handler")) {
3644 return 0;
3645 }
3646 for (Py_ssize_t i = 0; i < n; i++) {
3647 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
3648 s->v.TryStar.handlers, i);
3649 SET_LOC(c, handler);
3650 except = compiler_new_block(c);
3651 if (except == NULL) {
3652 return 0;
3653 }
3654 basicblock *handle_match = compiler_new_block(c);
3655 if (handle_match == NULL) {
3656 return 0;
3657 }
3658 if (i == 0) {
3659 /* Push the original EG into the stack */
3660 /*
3661 [exc] COPY 1
3662 [orig, exc]
3663 */
3664 ADDOP_I(c, COPY, 1);
3665
3666 /* create empty list for exceptions raised/reraise in the except* blocks */
3667 /*
3668 [orig, exc] BUILD_LIST
3669 [orig, exc, []] SWAP 2
3670 [orig, [], exc]
3671 */
3672 ADDOP_I(c, BUILD_LIST, 0);
3673 ADDOP_I(c, SWAP, 2);
3674 }
3675 if (handler->v.ExceptHandler.type) {
3676 VISIT(c, expr, handler->v.ExceptHandler.type);
3677 ADDOP(c, CHECK_EG_MATCH);
3678 ADDOP_I(c, COPY, 1);
3679 ADDOP_JUMP(c, POP_JUMP_IF_NOT_NONE, handle_match);
3680 ADDOP(c, POP_TOP); // match
3681 ADDOP_JUMP(c, JUMP, except);
3682 }
3683
3684 compiler_use_next_block(c, handle_match);
3685
3686 basicblock *cleanup_end = compiler_new_block(c);
3687 if (cleanup_end == NULL) {
3688 return 0;
3689 }
3690 basicblock *cleanup_body = compiler_new_block(c);
3691 if (cleanup_body == NULL) {
3692 return 0;
3693 }
3694
3695 if (handler->v.ExceptHandler.name) {
3696 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3697 }
3698 else {
3699 ADDOP(c, POP_TOP); // match
3700 }
3701
3702 /*
3703 try:
3704 # body
3705 except type as name:
3706 try:
3707 # body
3708 finally:
3709 name = None # in case body contains "del name"
3710 del name
3711 */
3712 /* second try: */
3713 ADDOP_JUMP(c, SETUP_CLEANUP, cleanup_end);
3714 compiler_use_next_block(c, cleanup_body);
3715 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
3716 return 0;
3717
3718 /* second # body */
3719 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
3720 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3721 /* name = None; del name; # Mark as artificial */
3722 UNSET_LOC(c);
3723 ADDOP(c, POP_BLOCK);
3724 if (handler->v.ExceptHandler.name) {
3725 ADDOP_LOAD_CONST(c, Py_None);
3726 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3727 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
3728 }
3729 ADDOP_JUMP(c, JUMP, except);
3730
3731 /* except: */
3732 compiler_use_next_block(c, cleanup_end);
3733
3734 /* name = None; del name; # Mark as artificial */
3735 UNSET_LOC(c);
3736
3737 if (handler->v.ExceptHandler.name) {
3738 ADDOP_LOAD_CONST(c, Py_None);
3739 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3740 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
3741 }
3742
3743 /* add exception raised to the res list */
3744 ADDOP_I(c, LIST_APPEND, 3); // exc
3745 ADDOP(c, POP_TOP); // lasti
3746
3747 ADDOP_JUMP(c, JUMP, except);
3748 compiler_use_next_block(c, except);
3749
3750 if (i == n - 1) {
3751 /* Add exc to the list (if not None it's the unhandled part of the EG) */
3752 ADDOP_I(c, LIST_APPEND, 1);
3753 ADDOP_JUMP(c, JUMP, reraise_star);
3754 }
3755 }
3756 /* Mark as artificial */
3757 UNSET_LOC(c);
3758 compiler_pop_fblock(c, EXCEPTION_GROUP_HANDLER, NULL);
3759 basicblock *reraise = compiler_new_block(c);
3760 if (!reraise) {
3761 return 0;
3762 }
3763
3764 compiler_use_next_block(c, reraise_star);
3765 ADDOP(c, PREP_RERAISE_STAR);
3766 ADDOP_I(c, COPY, 1);
3767 ADDOP_JUMP(c, POP_JUMP_IF_NOT_NONE, reraise);
3768
3769 /* Nothing to reraise */
3770 ADDOP(c, POP_TOP);
3771 ADDOP(c, POP_BLOCK);
3772 ADDOP(c, POP_EXCEPT);
3773 ADDOP_JUMP(c, JUMP, end);
3774 compiler_use_next_block(c, reraise);
3775 ADDOP(c, POP_BLOCK);
3776 ADDOP_I(c, SWAP, 2);
3777 ADDOP(c, POP_EXCEPT);
3778 ADDOP_I(c, RERAISE, 0);
3779 compiler_use_next_block(c, cleanup);
3780 POP_EXCEPT_AND_RERAISE(c);
3781 compiler_use_next_block(c, orelse);
3782 VISIT_SEQ(c, stmt, s->v.TryStar.orelse);
3783 compiler_use_next_block(c, end);
3784 return 1;
3785 }
3786
3787 static int
3788 13221 compiler_try(struct compiler *c, stmt_ty s) {
3789
4/6
✓ Branch 0 taken 2032 times.
✓ Branch 1 taken 11189 times.
✓ Branch 2 taken 2032 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2032 times.
✗ Branch 5 not taken.
13221 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3790 2032 return compiler_try_finally(c, s);
3791 else
3792 11189 return compiler_try_except(c, s);
3793 }
3794
3795 static int
3796 compiler_try_star(struct compiler *c, stmt_ty s)
3797 {
3798 if (s->v.TryStar.finalbody && asdl_seq_LEN(s->v.TryStar.finalbody)) {
3799 return compiler_try_star_finally(c, s);
3800 }
3801 else {
3802 return compiler_try_star_except(c, s);
3803 }
3804 }
3805
3806 static int
3807 491 compiler_import_as(struct compiler *c, identifier name, identifier asname)
3808 {
3809 /* The IMPORT_NAME opcode was already generated. This function
3810 merely needs to bind the result to a name.
3811
3812 If there is a dot in name, we need to split it and emit a
3813 IMPORT_FROM for each name.
3814 */
3815 491 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3816 491 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
3817
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 491 times.
491 if (dot == -2)
3818 return 0;
3819
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 241 times.
491 if (dot != -1) {
3820 /* Consume the base module name to get the first attribute */
3821 166 while (1) {
3822 416 Py_ssize_t pos = dot + 1;
3823 PyObject *attr;
3824 416 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
3825
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 416 times.
416 if (dot == -2)
3826 return 0;
3827
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 250 times.
416 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
3828
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 416 times.
416 if (!attr)
3829 return 0;
3830
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 416 times.
416 ADDOP_N(c, IMPORT_FROM, attr, names);
3831
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 166 times.
416 if (dot == -1) {
3832 250 break;
3833 }
3834
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 166 times.
166 ADDOP_I(c, SWAP, 2);
3835
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 166 times.
166 ADDOP(c, POP_TOP);
3836 }
3837
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 250 times.
250 if (!compiler_nameop(c, asname, Store)) {
3838 return 0;
3839 }
3840
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 250 times.
250 ADDOP(c, POP_TOP);
3841 250 return 1;
3842 }
3843 241 return compiler_nameop(c, asname, Store);
3844 }
3845
3846 static int
3847 22091 compiler_import(struct compiler *c, stmt_ty s)
3848 {
3849 /* The Import node stores a module name like a.b.c as a single
3850 string. This is convenient for all cases except
3851 import a.b.c as d
3852 where we need to parse that string to extract the individual
3853 module names.
3854 XXX Perhaps change the representation to make this case simpler?
3855 */
3856
1/2
✓ Branch 0 taken 22091 times.
✗ Branch 1 not taken.
22091 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
3857
3858 22091 PyObject *zero = _PyLong_GetZero(); // borrowed reference
3859
2/2
✓ Branch 0 taken 22237 times.
✓ Branch 1 taken 22091 times.
44328 for (i = 0; i < n; i++) {
3860 22237 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3861 int r;
3862
3863
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22237 times.
22237 ADDOP_LOAD_CONST(c, zero);
3864
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22237 times.
22237 ADDOP_LOAD_CONST(c, Py_None);
3865
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 22237 times.
22237 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
3866
3867
2/2
✓ Branch 0 taken 491 times.
✓ Branch 1 taken 21746 times.
22237 if (alias->asname) {
3868 491 r = compiler_import_as(c, alias->name, alias->asname);
3869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 491 times.
491 if (!r)
3870 return r;
3871 }
3872 else {
3873 21746 identifier tmp = alias->name;
3874 21746 Py_ssize_t dot = PyUnicode_FindChar(
3875 21746 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
3876
2/2
✓ Branch 0 taken 593 times.
✓ Branch 1 taken 21153 times.
21746 if (dot != -1) {
3877 593 tmp = PyUnicode_Substring(alias->name, 0, dot);
3878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 593 times.
593 if (tmp == NULL)
3879 return 0;
3880 }
3881 21746 r = compiler_nameop(c, tmp, Store);
3882
2/2
✓ Branch 0 taken 593 times.
✓ Branch 1 taken 21153 times.
21746 if (dot != -1) {
3883 593 Py_DECREF(tmp);
3884 }
3885
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21746 times.
21746 if (!r)
3886 return r;
3887 }
3888 }
3889 22091 return 1;
3890 }
3891
3892 static int
3893 33825 compiler_from_import(struct compiler *c, stmt_ty s)
3894 {
3895
1/2
✓ Branch 0 taken 33825 times.
✗ Branch 1 not taken.
33825 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
3896 PyObject *names;
3897
3898
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 33825 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 33825 times.
33825 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
3899
3900 33825 names = PyTuple_New(n);
3901
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33825 times.
33825 if (!names)
3902 return 0;
3903
3904 /* build up the names */
3905
2/2
✓ Branch 0 taken 83370 times.
✓ Branch 1 taken 33825 times.
117195 for (i = 0; i < n; i++) {
3906 83370 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3907 83370 Py_INCREF(alias->name);
3908 83370 PyTuple_SET_ITEM(names, i, alias->name);
3909 }
3910
3911
5/6
✓ Branch 0 taken 33462 times.
✓ Branch 1 taken 363 times.
✓ Branch 2 taken 32089 times.
✓ Branch 3 taken 1373 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 32089 times.
65914 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
3912 32089 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
3913 Py_DECREF(names);
3914 return compiler_error(c, "from __future__ imports must occur "
3915 "at the beginning of the file");
3916 }
3917
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 33825 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33825 times.
33825 ADDOP_LOAD_CONST_NEW(c, names);
3918
3919
2/2
✓ Branch 0 taken 32452 times.
✓ Branch 1 taken 1373 times.
33825 if (s->v.ImportFrom.module) {
3920
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32452 times.
32452 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3921 }
3922 else {
3923 _Py_DECLARE_STR(empty, "");
3924
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1373 times.
1373 ADDOP_NAME(c, IMPORT_NAME, &_Py_STR(empty), names);
3925 }
3926
2/2
✓ Branch 0 taken 83370 times.
✓ Branch 1 taken 32467 times.
115837 for (i = 0; i < n; i++) {
3927 83370 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3928 identifier store_name;
3929
3930
4/4
✓ Branch 0 taken 33825 times.
✓ Branch 1 taken 49545 times.
✓ Branch 3 taken 1358 times.
✓ Branch 4 taken 32467 times.
83370 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
3931 assert(n == 1);
3932
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1358 times.
1358 ADDOP(c, IMPORT_STAR);
3933 1358 return 1;
3934 }
3935
3936
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 82012 times.
82012 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3937 82012 store_name = alias->name;
3938
2/2
✓ Branch 0 taken 1526 times.
✓ Branch 1 taken 80486 times.
82012 if (alias->asname)
3939 1526 store_name = alias->asname;
3940
3941
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 82012 times.
82012 if (!compiler_nameop(c, store_name, Store)) {
3942 return 0;
3943 }
3944 }
3945 /* remove imported module */
3946
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 32467 times.
32467 ADDOP(c, POP_TOP);
3947 32467 return 1;
3948 }
3949
3950 static int
3951 77862 compiler_assert(struct compiler *c, stmt_ty s)
3952 {
3953 basicblock *end;
3954
3955 /* Always emit a warning if the test is a non-zero length tuple */
3956
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77862 times.
77862 if ((s->v.Assert.test->kind == Tuple_kind &&
3957 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3958
3/4
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 77753 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 109 times.
77971 (s->v.Assert.test->kind == Constant_kind &&
3959
0/2
✗ Branch 2 not taken.
✗ Branch 3 not taken.
109 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3960 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
3961 {
3962 if (!compiler_warn(c, "assertion is always true, "
3963 "perhaps remove parentheses?"))
3964 {
3965 return 0;
3966 }
3967 }
3968
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77862 times.
77862 if (c->c_optimize)
3969 return 1;
3970 77862 end = compiler_new_block(c);
3971
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77862 times.
77862 if (end == NULL)
3972 return 0;
3973
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77862 times.
77862 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3974 return 0;
3975
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77862 times.
77862 ADDOP(c, LOAD_ASSERTION_ERROR);
3976
2/2
✓ Branch 0 taken 833 times.
✓ Branch 1 taken 77029 times.
77862 if (s->v.Assert.msg) {
3977
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 833 times.
833 VISIT(c, expr, s->v.Assert.msg);
3978
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 833 times.
833 ADDOP_I(c, CALL, 0);
3979 }
3980
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77862 times.
77862 ADDOP_I(c, RAISE_VARARGS, 1);
3981 77862 compiler_use_next_block(c, end);
3982 77862 return 1;
3983 }
3984
3985 static int
3986 161761 compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3987 {
3988
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 161761 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
161761 if (c->c_interactive && c->c_nestlevel <= 1) {
3989 VISIT(c, expr, value);
3990 ADDOP(c, PRINT_EXPR);
3991 return 1;
3992 }
3993
3994
2/2
✓ Branch 0 taken 891 times.
✓ Branch 1 taken 160870 times.
161761 if (value->kind == Constant_kind) {
3995 /* ignore constant statement */
3996
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 891 times.
891 ADDOP(c, NOP);
3997 891 return 1;
3998 }
3999
4000
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 160870 times.
160870 VISIT(c, expr, value);
4001 /* Mark POP_TOP as artificial */
4002 160870 UNSET_LOC(c);
4003
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 160870 times.
160870 ADDOP(c, POP_TOP);
4004 160870 return 1;
4005 }
4006
4007 static int
4008 1287939 compiler_visit_stmt(struct compiler *c, stmt_ty s)
4009 {
4010 Py_ssize_t i, n;
4011
4012 /* Always assign a lineno to the next instruction for a stmt. */
4013 1287939 SET_LOC(c, s);
4014
4015
25/27
✓ Branch 0 taken 159636 times.
✓ Branch 1 taken 15204 times.
✓ Branch 2 taken 150682 times.
✓ Branch 3 taken 1652 times.
✓ Branch 4 taken 421319 times.
✓ Branch 5 taken 15223 times.
✓ Branch 6 taken 2553 times.
✓ Branch 7 taken 33890 times.
✓ Branch 8 taken 4100 times.
✓ Branch 9 taken 137065 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 20907 times.
✓ Branch 12 taken 13221 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 77862 times.
✓ Branch 15 taken 22091 times.
✓ Branch 16 taken 33825 times.
✓ Branch 17 taken 530 times.
✓ Branch 18 taken 161761 times.
✓ Branch 19 taken 5399 times.
✓ Branch 20 taken 2764 times.
✓ Branch 21 taken 2799 times.
✓ Branch 22 taken 4330 times.
✓ Branch 23 taken 1106 times.
✓ Branch 24 taken 14 times.
✓ Branch 25 taken 3 times.
✗ Branch 26 not taken.
1287939 switch (s->kind) {
4016 159636 case FunctionDef_kind:
4017 159636 return compiler_function(c, s, 0);
4018 15204 case ClassDef_kind:
4019 15204 return compiler_class(c, s);
4020 150682 case Return_kind:
4021 150682 return compiler_return(c, s);
4022 1652 case Delete_kind:
4023
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 1764 times.
✓ Branch 3 taken 3416 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1764 times.
✓ Branch 6 taken 1652 times.
3416 VISIT_SEQ(c, expr, s->v.Delete.targets)
4024 1652 break;
4025 421319 case Assign_kind:
4026
1/2
✓ Branch 0 taken 421319 times.
✗ Branch 1 not taken.
421319 n = asdl_seq_LEN(s->v.Assign.targets);
4027
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 421319 times.
421319 VISIT(c, expr, s->v.Assign.value);
4028
2/2
✓ Branch 0 taken 423439 times.
✓ Branch 1 taken 421319 times.
844758 for (i = 0; i < n; i++) {
4029
2/2
✓ Branch 0 taken 2120 times.
✓ Branch 1 taken 421319 times.
423439 if (i < n - 1) {
4030
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2120 times.
2120 ADDOP_I(c, COPY, 1);
4031 }
4032
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 423439 times.
423439 VISIT(c, expr,
4033 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
4034 }
4035 421319 break;
4036 15223 case AugAssign_kind:
4037 15223 return compiler_augassign(c, s);
4038 2553 case AnnAssign_kind:
4039 2553 return compiler_annassign(c, s);
4040 33890 case For_kind:
4041 33890 return compiler_for(c, s);
4042 4100 case While_kind:
4043 4100 return compiler_while(c, s);
4044 137065 case If_kind:
4045 137065 return compiler_if(c, s);
4046 3 case Match_kind:
4047 3 return compiler_match(c, s);
4048 20907 case Raise_kind:
4049 20907 n = 0;
4050
2/2
✓ Branch 0 taken 19699 times.
✓ Branch 1 taken 1208 times.
20907 if (s->v.Raise.exc) {
4051
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19699 times.
19699 VISIT(c, expr, s->v.Raise.exc);
4052 19699 n++;
4053
2/2
✓ Branch 0 taken 967 times.
✓ Branch 1 taken 18732 times.
19699 if (s->v.Raise.cause) {
4054
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 967 times.
967 VISIT(c, expr, s->v.Raise.cause);
4055 967 n++;
4056 }
4057 }
4058
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20907 times.
20907 ADDOP_I(c, RAISE_VARARGS, (int)n);
4059 20907 break;
4060 13221 case Try_kind:
4061 13221 return compiler_try(c, s);
4062 case TryStar_kind:
4063 return compiler_try_star(c, s);
4064 77862 case Assert_kind:
4065 77862 return compiler_assert(c, s);
4066 22091 case Import_kind:
4067 22091 return compiler_import(c, s);
4068 33825 case ImportFrom_kind:
4069 33825 return compiler_from_import(c, s);
4070 530 case Global_kind:
4071 case Nonlocal_kind:
4072 530 break;
4073 161761 case Expr_kind:
4074 161761 return compiler_visit_stmt_expr(c, s->v.Expr.value);
4075 5399 case Pass_kind:
4076
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5399 times.
5399 ADDOP(c, NOP);
4077 5399 break;
4078 2764 case Break_kind:
4079 2764 return compiler_break(c);
4080 2799 case Continue_kind:
4081 2799 return compiler_continue(c);
4082 4330 case With_kind:
4083 4330 return compiler_with(c, s, 0);
4084 1106 case AsyncFunctionDef_kind:
4085 1106 return compiler_function(c, s, 1);
4086 14 case AsyncWith_kind:
4087 14 return compiler_async_with(c, s, 0);
4088 3 case AsyncFor_kind:
4089 3 return compiler_async_for(c, s);
4090 }
4091
4092 449807 return 1;
4093 }
4094
4095 static int
4096 47921 unaryop(unaryop_ty op)
4097 {
4098
4/5
✓ Branch 0 taken 1028 times.
✓ Branch 1 taken 1635 times.
✓ Branch 2 taken 149 times.
✓ Branch 3 taken 45109 times.
✗ Branch 4 not taken.
47921 switch (op) {
4099 1028 case Invert:
4100 1028 return UNARY_INVERT;
4101 1635 case Not:
4102 1635 return UNARY_NOT;
4103 149 case UAdd:
4104 149 return UNARY_POSITIVE;
4105 45109 case USub:
4106 45109 return UNARY_NEGATIVE;
4107 default:
4108 PyErr_Format(PyExc_SystemError,
4109 "unary op %d should not be possible", op);
4110 return 0;
4111 }
4112 }
4113
4114 static int
4115 1183551 addop_binary(struct compiler *c, operator_ty binop, bool inplace)
4116 {
4117 int oparg;
4118
13/14
✓ Branch 0 taken 287426 times.
✓ Branch 1 taken 68513 times.
✓ Branch 2 taken 471841 times.
✓ Branch 3 taken 17 times.
✓ Branch 4 taken 126196 times.
✓ Branch 5 taken 13581 times.
✓ Branch 6 taken 204773 times.
✓ Branch 7 taken 1595 times.
✓ Branch 8 taken 1405 times.
✓ Branch 9 taken 2298 times.
✓ Branch 10 taken 396 times.
✓ Branch 11 taken 3544 times.
✓ Branch 12 taken 1966 times.
✗ Branch 13 not taken.
1183551 switch (binop) {
4119 287426 case Add:
4120
2/2
✓ Branch 0 taken 10626 times.
✓ Branch 1 taken 276800 times.
287426 oparg = inplace ? NB_INPLACE_ADD : NB_ADD;
4121 287426 break;
4122 68513 case Sub:
4123
2/2
✓ Branch 0 taken 2084 times.
✓ Branch 1 taken 66429 times.
68513 oparg = inplace ? NB_INPLACE_SUBTRACT : NB_SUBTRACT;
4124 68513 break;
4125 471841 case Mult:
4126
2/2
✓ Branch 0 taken 1084 times.
✓ Branch 1 taken 470757 times.
471841 oparg = inplace ? NB_INPLACE_MULTIPLY : NB_MULTIPLY;
4127 471841 break;
4128 17 case MatMult:
4129
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 16 times.
17 oparg = inplace ? NB_INPLACE_MATRIX_MULTIPLY : NB_MATRIX_MULTIPLY;
4130 17 break;
4131 126196 case Div:
4132
2/2
✓ Branch 0 taken 212 times.
✓ Branch 1 taken 125984 times.
126196 oparg = inplace ? NB_INPLACE_TRUE_DIVIDE : NB_TRUE_DIVIDE;
4133 126196 break;
4134 13581 case Mod:
4135
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 13539 times.
13581 oparg = inplace ? NB_INPLACE_REMAINDER : NB_REMAINDER;
4136 13581 break;
4137 204773 case Pow:
4138
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 204770 times.
204773 oparg = inplace ? NB_INPLACE_POWER : NB_POWER;
4139 204773 break;
4140 1595 case LShift:
4141
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 1420 times.
1595 oparg = inplace ? NB_INPLACE_LSHIFT : NB_LSHIFT;
4142 1595 break;
4143 1405 case RShift:
4144
2/2
✓ Branch 0 taken 286 times.
✓ Branch 1 taken 1119 times.
1405 oparg = inplace ? NB_INPLACE_RSHIFT : NB_RSHIFT;
4145 1405 break;
4146 2298 case BitOr:
4147
2/2
✓ Branch 0 taken 389 times.
✓ Branch 1 taken 1909 times.
2298 oparg = inplace ? NB_INPLACE_OR : NB_OR;
4148 2298 break;
4149 396 case BitXor:
4150
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 299 times.
396 oparg = inplace ? NB_INPLACE_XOR : NB_XOR;
4151 396 break;
4152 3544 case BitAnd:
4153
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 3421 times.
3544 oparg = inplace ? NB_INPLACE_AND : NB_AND;
4154 3544 break;
4155 1966 case FloorDiv:
4156
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 1865 times.
1966 oparg = inplace ? NB_INPLACE_FLOOR_DIVIDE : NB_FLOOR_DIVIDE;
4157 1966 break;
4158 default:
4159 PyErr_Format(PyExc_SystemError, "%s op %d should not be possible",
4160 inplace ? "inplace" : "binary", binop);
4161 return 0;
4162 }
4163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1183551 times.
1183551 ADDOP_I(c, BINARY_OP, oparg);
4164 1183551 return 1;
4165 }
4166
4167
4168 static int
4169 10259 addop_yield(struct compiler *c) {
4170
3/4
✓ Branch 0 taken 10259 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 10251 times.
10259 if (c->u->u_ste->ste_generator && c->u->u_ste->ste_coroutine) {
4171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 ADDOP(c, ASYNC_GEN_WRAP);
4172 }
4173
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10259 times.
10259 ADDOP_I(c, YIELD_VALUE, 0);
4174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10259 times.
10259 ADDOP_I(c, RESUME, 1);
4175 10259 return 1;
4176 }
4177
4178 static int
4179 5174336 compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
4180 {
4181 int op, scope;
4182 Py_ssize_t arg;
4183 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
4184
4185 5174336 PyObject *dict = c->u->u_names;
4186 PyObject *mangled;
4187
4188 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
4189 !_PyUnicode_EqualToASCIIString(name, "True") &&
4190 !_PyUnicode_EqualToASCIIString(name, "False"));
4191
4192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5174336 times.
5174336 if (forbidden_name(c, name, ctx))
4193 return 0;
4194
4195 5174336 mangled = _Py_Mangle(c->u->u_private, name);
4196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5174336 times.
5174336 if (!mangled)
4197 return 0;
4198
4199 5174336 op = 0;
4200 5174336 optype = OP_NAME;
4201 5174336 scope = _PyST_GetScope(c->u->u_ste, mangled);
4202
6/6
✓ Branch 0 taken 28436 times.
✓ Branch 1 taken 58657 times.
✓ Branch 2 taken 3186878 times.
✓ Branch 3 taken 1839467 times.
✓ Branch 4 taken 1867 times.
✓ Branch 5 taken 59031 times.
5174336 switch (scope) {
4203 28436 case FREE:
4204 28436 dict = c->u->u_freevars;
4205 28436 optype = OP_DEREF;
4206 28436 break;
4207 58657 case CELL:
4208 58657 dict = c->u->u_cellvars;
4209 58657 optype = OP_DEREF;
4210 58657 break;
4211 3186878 case LOCAL:
4212
2/2
✓ Branch 0 taken 2744759 times.
✓ Branch 1 taken 442119 times.
3186878 if (c->u->u_ste->ste_type == FunctionBlock)
4213 2744759 optype = OP_FAST;
4214 3186878 break;
4215 1839467 case GLOBAL_IMPLICIT:
4216
2/2
✓ Branch 0 taken 1748995 times.
✓ Branch 1 taken 90472 times.
1839467 if (c->u->u_ste->ste_type == FunctionBlock)
4217 1748995 optype = OP_GLOBAL;
4218 1839467 break;
4219 1867 case GLOBAL_EXPLICIT:
4220 1867 optype = OP_GLOBAL;
4221 1867 break;
4222 59031 default:
4223 /* scope can be 0 */
4224 59031 break;
4225 }
4226
4227 /* XXX Leave assert here, but handle __doc__ and the like better */
4228 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
4229
4230
4/5
✓ Branch 0 taken 87093 times.
✓ Branch 1 taken 2744759 times.
✓ Branch 2 taken 1750862 times.
✓ Branch 3 taken 591622 times.
✗ Branch 4 not taken.
5174336 switch (optype) {
4231
3/4
✓ Branch 0 taken 75392 times.
✓ Branch 1 taken 11697 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
87093 case OP_DEREF:
4232 switch (ctx) {
4233 75392 case Load:
4234
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 75270 times.
75392 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
4235 75392 break;
4236 11697 case Store: op = STORE_DEREF; break;
4237 4 case Del: op = DELETE_DEREF; break;
4238 }
4239 87093 break;
4240
3/4
✓ Branch 0 taken 2266610 times.
✓ Branch 1 taken 473647 times.
✓ Branch 2 taken 4502 times.
✗ Branch 3 not taken.
2744759 case OP_FAST:
4241 switch (ctx) {
4242 2266610 case Load: op = LOAD_FAST; break;
4243 473647 case Store: op = STORE_FAST; break;
4244 4502 case Del: op = DELETE_FAST; break;
4245 }
4246
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2744759 times.
2744759 ADDOP_N(c, op, mangled, varnames);
4247 2744759 return 1;
4248
3/4
✓ Branch 0 taken 1749889 times.
✓ Branch 1 taken 969 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
1750862 case OP_GLOBAL:
4249 switch (ctx) {
4250 1749889 case Load: op = LOAD_GLOBAL; break;
4251 969 case Store: op = STORE_GLOBAL; break;
4252 4 case Del: op = DELETE_GLOBAL; break;
4253 }
4254 1750862 break;
4255
3/4
✓ Branch 0 taken 225166 times.
✓ Branch 1 taken 366072 times.
✓ Branch 2 taken 384 times.
✗ Branch 3 not taken.
591622 case OP_NAME:
4256 switch (ctx) {
4257 225166 case Load: op = LOAD_NAME; break;
4258 366072 case Store: op = STORE_NAME; break;
4259 384 case Del: op = DELETE_NAME; break;
4260 }
4261 591622 break;
4262 }
4263
4264 assert(op);
4265 2429577 arg = compiler_add_o(dict, mangled);
4266 2429577 Py_DECREF(mangled);
4267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2429577 times.
2429577 if (arg < 0) {
4268 return 0;
4269 }
4270
2/2
✓ Branch 0 taken 1749889 times.
✓ Branch 1 taken 679688 times.
2429577 if (op == LOAD_GLOBAL) {
4271 1749889 arg <<= 1;
4272 }
4273 2429577 return compiler_addop_i(c, op, arg, true);
4274 }
4275
4276 static int
4277 12787 compiler_boolop(struct compiler *c, expr_ty e)
4278 {
4279 basicblock *end;
4280 int jumpi;
4281 Py_ssize_t i, n;
4282 asdl_expr_seq *s;
4283
4284 assert(e->kind == BoolOp_kind);
4285
2/2
✓ Branch 0 taken 8321 times.
✓ Branch 1 taken 4466 times.
12787 if (e->v.BoolOp.op == And)
4286 8321 jumpi = JUMP_IF_FALSE_OR_POP;
4287 else
4288 4466 jumpi = JUMP_IF_TRUE_OR_POP;
4289 12787 end = compiler_new_block(c);
4290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12787 times.
12787 if (end == NULL)
4291 return 0;
4292 12787 s = e->v.BoolOp.values;
4293
1/2
✓ Branch 0 taken 12787 times.
✗ Branch 1 not taken.
12787 n = asdl_seq_LEN(s) - 1;
4294 assert(n >= 0);
4295
2/2
✓ Branch 0 taken 13687 times.
✓ Branch 1 taken 12787 times.
26474 for (i = 0; i < n; ++i) {
4296
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13687 times.
13687 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
4297
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13687 times.
13687 ADDOP_JUMP(c, jumpi, end);
4298 13687 basicblock *next = compiler_new_block(c);
4299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13687 times.
13687 if (next == NULL) {
4300 return 0;
4301 }
4302 13687 compiler_use_next_block(c, next);
4303 }
4304
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12787 times.
12787 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
4305 12787 compiler_use_next_block(c, end);
4306 12787 return 1;
4307 }
4308
4309 static int
4310 239396 starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
4311 int build, int add, int extend, int tuple)
4312 {
4313
2/2
✓ Branch 0 taken 225855 times.
✓ Branch 1 taken 13541 times.
239396 Py_ssize_t n = asdl_seq_LEN(elts);
4314
4/4
✓ Branch 0 taken 126937 times.
✓ Branch 1 taken 112459 times.
✓ Branch 3 taken 10746 times.
✓ Branch 4 taken 116191 times.
239396 if (n > 2 && are_all_items_const(elts, 0, n)) {
4315 10746 PyObject *folded = PyTuple_New(n);
4316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10746 times.
10746 if (folded == NULL) {
4317 return 0;
4318 }
4319 PyObject *val;
4320
2/2
✓ Branch 0 taken 91786 times.
✓ Branch 1 taken 10746 times.
102532 for (Py_ssize_t i = 0; i < n; i++) {
4321 91786 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
4322 91786 Py_INCREF(val);
4323 91786 PyTuple_SET_ITEM(folded, i, val);
4324 }
4325
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10745 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
10746 if (tuple && !pushed) {
4326
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 ADDOP_LOAD_CONST_NEW(c, folded);
4327 } else {
4328
2/2
✓ Branch 0 taken 187 times.
✓ Branch 1 taken 10558 times.
10745 if (add == SET_ADD) {
4329 187 Py_SETREF(folded, PyFrozenSet_New(folded));
4330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 187 times.
187 if (folded == NULL) {
4331 return 0;
4332 }
4333 }
4334
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10745 times.
10745 ADDOP_I(c, build, pushed);
4335
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10745 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10745 times.
10745 ADDOP_LOAD_CONST_NEW(c, folded);
4336
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10745 times.
10745 ADDOP_I(c, extend, 1);
4337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10745 times.
10745 if (tuple) {
4338 ADDOP(c, LIST_TO_TUPLE);
4339 }
4340 }
4341 10746 return 1;
4342 }
4343
4344 228650 int big = n+pushed > STACK_USE_GUIDELINE;
4345 228650 int seen_star = 0;
4346
2/2
✓ Branch 0 taken 768347 times.
✓ Branch 1 taken 223658 times.
992005 for (Py_ssize_t i = 0; i < n; i++) {
4347 768347 expr_ty elt = asdl_seq_GET(elts, i);
4348
2/2
✓ Branch 0 taken 4992 times.
✓ Branch 1 taken 763355 times.
768347 if (elt->kind == Starred_kind) {
4349 4992 seen_star = 1;
4350 4992 break;
4351 }
4352 }
4353
4/4
✓ Branch 0 taken 223658 times.
✓ Branch 1 taken 4992 times.
✓ Branch 2 taken 223546 times.
✓ Branch 3 taken 112 times.
228650 if (!seen_star && !big) {
4354
2/2
✓ Branch 0 taken 739384 times.
✓ Branch 1 taken 223546 times.
962930 for (Py_ssize_t i = 0; i < n; i++) {
4355 739384 expr_ty elt = asdl_seq_GET(elts, i);
4356
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 739384 times.
739384 VISIT(c, expr, elt);
4357 }
4358
2/2
✓ Branch 0 taken 168998 times.
✓ Branch 1 taken 54548 times.
223546 if (tuple) {
4359
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 168998 times.
168998 ADDOP_I(c, BUILD_TUPLE, n+pushed);
4360 } else {
4361
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 54548 times.
54548 ADDOP_I(c, build, n+pushed);
4362 }
4363 223546 return 1;
4364 }
4365 5104 int sequence_built = 0;
4366
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 4992 times.
5104 if (big) {
4367
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 112 times.
112 ADDOP_I(c, build, pushed);
4368 112 sequence_built = 1;
4369 }
4370
2/2
✓ Branch 0 taken 29250 times.
✓ Branch 1 taken 5104 times.
34354 for (Py_ssize_t i = 0; i < n; i++) {
4371 29250 expr_ty elt = asdl_seq_GET(elts, i);
4372
2/2
✓ Branch 0 taken 5157 times.
✓ Branch 1 taken 24093 times.
29250 if (elt->kind == Starred_kind) {
4373
2/2
✓ Branch 0 taken 4992 times.
✓ Branch 1 taken 165 times.
5157 if (sequence_built == 0) {
4374
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4992 times.
4992 ADDOP_I(c, build, i+pushed);
4375 4992 sequence_built = 1;
4376 }
4377
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5157 times.
5157 VISIT(c, expr, elt->v.Starred.value);
4378
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5157 times.
5157 ADDOP_I(c, extend, 1);
4379 }
4380 else {
4381
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24093 times.
24093 VISIT(c, expr, elt);
4382
2/2
✓ Branch 0 taken 15784 times.
✓ Branch 1 taken 8309 times.
24093 if (sequence_built) {
4383
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15784 times.
15784 ADDOP_I(c, add, 1);
4384 }
4385 }
4386 }
4387 assert(sequence_built);
4388
2/2
✓ Branch 0 taken 4919 times.
✓ Branch 1 taken 185 times.
5104 if (tuple) {
4389
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4919 times.
4919 ADDOP(c, LIST_TO_TUPLE);
4390 }
4391 5104 return 1;
4392 }
4393
4394 static int
4395 39437 unpack_helper(struct compiler *c, asdl_expr_seq *elts)
4396 {
4397
1/2
✓ Branch 0 taken 39437 times.
✗ Branch 1 not taken.
39437 Py_ssize_t n = asdl_seq_LEN(elts);
4398 39437 int seen_star = 0;
4399
2/2
✓ Branch 0 taken 162641 times.
✓ Branch 1 taken 39437 times.
202078 for (Py_ssize_t i = 0; i < n; i++) {
4400 162641 expr_ty elt = asdl_seq_GET(elts, i);
4401
3/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 162604 times.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
162641 if (elt->kind == Starred_kind && !seen_star) {
4402
1/2
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
37 if ((i >= (1 << 8)) ||
4403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 (n-i-1 >= (INT_MAX >> 8)))
4404 return compiler_error(c,
4405 "too many expressions in "
4406 "star-unpacking assignment");
4407
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
37 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
4408 37 seen_star = 1;
4409 }
4410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 162604 times.
162604 else if (elt->kind == Starred_kind) {
4411 return compiler_error(c,
4412 "multiple starred expressions in assignment");
4413 }
4414 }
4415
2/2
✓ Branch 0 taken 39400 times.
✓ Branch 1 taken 37 times.
39437 if (!seen_star) {
4416
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39400 times.
39400 ADDOP_I(c, UNPACK_SEQUENCE, n);
4417 }
4418 39437 return 1;
4419 }
4420
4421 static int
4422 39437 assignment_helper(struct compiler *c, asdl_expr_seq *elts)
4423 {
4424
1/2
✓ Branch 0 taken 39437 times.
✗ Branch 1 not taken.
39437 Py_ssize_t n = asdl_seq_LEN(elts);
4425
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39437 times.
39437 RETURN_IF_FALSE(unpack_helper(c, elts));
4426
2/2
✓ Branch 0 taken 162641 times.
✓ Branch 1 taken 39437 times.
202078 for (Py_ssize_t i = 0; i < n; i++) {
4427 162641 expr_ty elt = asdl_seq_GET(elts, i);
4428
3/4
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 162604 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 162641 times.
162641 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
4429 }
4430 39437 return 1;
4431 }
4432
4433 static int
4434 64045 compiler_list(struct compiler *c, expr_ty e)
4435 {
4436 64045 asdl_expr_seq *elts = e->v.List.elts;
4437
2/2
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 63869 times.
64045 if (e->v.List.ctx == Store) {
4438 176 return assignment_helper(c, elts);
4439 }
4440
1/2
✓ Branch 0 taken 63869 times.
✗ Branch 1 not taken.
63869 else if (e->v.List.ctx == Load) {
4441 63869 return starunpack_helper(c, elts, 0, BUILD_LIST,
4442 LIST_APPEND, LIST_EXTEND, 0);
4443 }
4444 else
4445 VISIT_SEQ(c, expr, elts);
4446 return 1;
4447 }
4448
4449 static int
4450 201684 compiler_tuple(struct compiler *c, expr_ty e)
4451 {
4452 201684 asdl_expr_seq *elts = e->v.Tuple.elts;
4453
2/2
✓ Branch 0 taken 39261 times.
✓ Branch 1 taken 162423 times.
201684 if (e->v.Tuple.ctx == Store) {
4454 39261 return assignment_helper(c, elts);
4455 }
4456
2/2
✓ Branch 0 taken 162422 times.
✓ Branch 1 taken 1 times.
162423 else if (e->v.Tuple.ctx == Load) {
4457 162422 return starunpack_helper(c, elts, 0, BUILD_LIST,
4458 LIST_APPEND, LIST_EXTEND, 1);
4459 }
4460 else
4461
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✓ Branch 3 taken 43 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 42 times.
✓ Branch 6 taken 1 times.
43 VISIT_SEQ(c, expr, elts);
4462 1 return 1;
4463 }
4464
4465 static int
4466 1609 compiler_set(struct compiler *c, expr_ty e)
4467 {
4468 1609 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
4469 SET_ADD, SET_UPDATE, 0);
4470 }
4471
4472 static int
4473 136036 are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
4474 {
4475 Py_ssize_t i;
4476
2/2
✓ Branch 0 taken 256516 times.
✓ Branch 1 taken 18472 times.
274988 for (i = begin; i < end; i++) {
4477 256516 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
4478
3/4
✓ Branch 0 taken 256516 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 117564 times.
✓ Branch 3 taken 138952 times.
256516 if (key == NULL || key->kind != Constant_kind)
4479 117564 return 0;
4480 }
4481 18472 return 1;
4482 }
4483
4484 static int
4485 20807 compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
4486 {
4487 20807 Py_ssize_t i, n = end - begin;
4488 PyObject *keys, *key;
4489 20807 int big = n*2 > STACK_USE_GUIDELINE;
4490
6/6
✓ Branch 0 taken 16033 times.
✓ Branch 1 taken 4774 times.
✓ Branch 2 taken 9099 times.
✓ Branch 3 taken 6934 times.
✓ Branch 5 taken 7726 times.
✓ Branch 6 taken 1373 times.
20807 if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
4491
2/2
✓ Branch 0 taken 36881 times.
✓ Branch 1 taken 7726 times.
44607 for (i = begin; i < end; i++) {
4492
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 36881 times.
36881 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4493 }
4494 7726 keys = PyTuple_New(n);
4495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7726 times.
7726 if (keys == NULL) {
4496 return 0;
4497 }
4498
2/2
✓ Branch 0 taken 36881 times.
✓ Branch 1 taken 7726 times.
44607 for (i = begin; i < end; i++) {
4499 36881 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
4500 36881 Py_INCREF(key);
4501 36881 PyTuple_SET_ITEM(keys, i - begin, key);
4502 }
4503
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7726 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7726 times.
7726 ADDOP_LOAD_CONST_NEW(c, keys);
4504
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7726 times.
7726 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4505 7726 return 1;
4506 }
4507
2/2
✓ Branch 0 taken 6934 times.
✓ Branch 1 taken 6147 times.
13081 if (big) {
4508
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6934 times.
6934 ADDOP_I(c, BUILD_MAP, 0);
4509 }
4510
2/2
✓ Branch 0 taken 126940 times.
✓ Branch 1 taken 13081 times.
140021 for (i = begin; i < end; i++) {
4511
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 126940 times.
126940 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
4512
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 126940 times.
126940 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4513
2/2
✓ Branch 0 taken 117693 times.
✓ Branch 1 taken 9247 times.
126940 if (big) {
4514
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 117693 times.
117693 ADDOP_I(c, MAP_ADD, 1);
4515 }
4516 }
4517
2/2
✓ Branch 0 taken 6147 times.
✓ Branch 1 taken 6934 times.
13081 if (!big) {
4518
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6147 times.
6147 ADDOP_I(c, BUILD_MAP, n);
4519 }
4520 13081 return 1;
4521 }
4522
4523 static int
4524 19396 compiler_dict(struct compiler *c, expr_ty e)
4525 {
4526 Py_ssize_t i, n, elements;
4527 int have_dict;
4528 19396 int is_unpacking = 0;
4529
1/2
✓ Branch 0 taken 19396 times.
✗ Branch 1 not taken.
19396 n = asdl_seq_LEN(e->v.Dict.values);
4530 19396 have_dict = 0;
4531 19396 elements = 0;
4532
2/2
✓ Branch 0 taken 164042 times.
✓ Branch 1 taken 19396 times.
183438 for (i = 0; i < n; i++) {
4533 164042 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
4534
2/2
✓ Branch 0 taken 221 times.
✓ Branch 1 taken 163821 times.
164042 if (is_unpacking) {
4535
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 153 times.
221 if (elements) {
4536
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
68 if (!compiler_subdict(c, e, i - elements, i)) {
4537 return 0;
4538 }
4539
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 62 times.
68 if (have_dict) {
4540
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 ADDOP_I(c, DICT_UPDATE, 1);
4541 }
4542 68 have_dict = 1;
4543 68 elements = 0;
4544 }
4545
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 122 times.
221 if (have_dict == 0) {
4546
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
99 ADDOP_I(c, BUILD_MAP, 0);
4547 99 have_dict = 1;
4548 }
4549
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 221 times.
221 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4550
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 221 times.
221 ADDOP_I(c, DICT_UPDATE, 1);
4551 }
4552 else {
4553
2/2
✓ Branch 0 taken 6749 times.
✓ Branch 1 taken 157072 times.
163821 if (elements*2 > STACK_USE_GUIDELINE) {
4554
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6749 times.
6749 if (!compiler_subdict(c, e, i - elements, i + 1)) {
4555 return 0;
4556 }
4557
2/2
✓ Branch 0 taken 5069 times.
✓ Branch 1 taken 1680 times.
6749 if (have_dict) {
4558
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5069 times.
5069 ADDOP_I(c, DICT_UPDATE, 1);
4559 }
4560 6749 have_dict = 1;
4561 6749 elements = 0;
4562 }
4563 else {
4564 157072 elements++;
4565 }
4566 }
4567 }
4568
2/2
✓ Branch 0 taken 13990 times.
✓ Branch 1 taken 5406 times.
19396 if (elements) {
4569
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13990 times.
13990 if (!compiler_subdict(c, e, n - elements, n)) {
4570 return 0;
4571 }
4572
2/2
✓ Branch 0 taken 1692 times.
✓ Branch 1 taken 12298 times.
13990 if (have_dict) {
4573
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1692 times.
1692 ADDOP_I(c, DICT_UPDATE, 1);
4574 }
4575 13990 have_dict = 1;
4576 }
4577
2/2
✓ Branch 0 taken 5257 times.
✓ Branch 1 taken 14139 times.
19396 if (!have_dict) {
4578
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5257 times.
5257 ADDOP_I(c, BUILD_MAP, 0);
4579 }
4580 19396 return 1;
4581 }
4582
4583 static int
4584 169368 compiler_compare(struct compiler *c, expr_ty e)
4585 {
4586 Py_ssize_t i, n;
4587
4588
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 169368 times.
169368 if (!check_compare(c, e)) {
4589 return 0;
4590 }
4591
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 169368 times.
169368 VISIT(c, expr, e->v.Compare.left);
4592 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4593
1/2
✓ Branch 0 taken 169368 times.
✗ Branch 1 not taken.
169368 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4594
2/2
✓ Branch 0 taken 169296 times.
✓ Branch 1 taken 72 times.
169368 if (n == 0) {
4595
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 169296 times.
169296 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
4596
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 169296 times.
169296 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
4597 }
4598 else {
4599 72 basicblock *cleanup = compiler_new_block(c);
4600
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (cleanup == NULL)
4601 return 0;
4602
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 72 times.
144 for (i = 0; i < n; i++) {
4603
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 VISIT(c, expr,
4604 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
4605
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 ADDOP_I(c, SWAP, 2);
4606
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 ADDOP_I(c, COPY, 2);
4607
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
4608
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
4609 }
4610
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
4611
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
4612 72 basicblock *end = compiler_new_block(c);
4613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 72 times.
72 if (end == NULL)
4614 return 0;
4615
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 ADDOP_JUMP_NOLINE(c, JUMP, end);
4616 72 compiler_use_next_block(c, cleanup);
4617
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 ADDOP_I(c, SWAP, 2);
4618
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 72 times.
72 ADDOP(c, POP_TOP);
4619 72 compiler_use_next_block(c, end);
4620 }
4621 169368 return 1;
4622 }
4623
4624 static PyTypeObject *
4625 132329 infer_type(expr_ty e)
4626 {
4627
5/9
✓ Branch 0 taken 5884 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 87121 times.
✓ Branch 8 taken 39319 times.
132329 switch (e->kind) {
4628 5884 case Tuple_kind:
4629 5884 return &PyTuple_Type;
4630 2 case List_kind:
4631 case ListComp_kind:
4632 2 return &PyList_Type;
4633 case Dict_kind:
4634 case DictComp_kind:
4635 return &PyDict_Type;
4636 case Set_kind:
4637 case SetComp_kind:
4638 return &PySet_Type;
4639 case GeneratorExp_kind:
4640 return &PyGen_Type;
4641 case Lambda_kind:
4642 return &PyFunction_Type;
4643 3 case JoinedStr_kind:
4644 case FormattedValue_kind:
4645 3 return &PyUnicode_Type;
4646 87121 case Constant_kind:
4647 87121 return Py_TYPE(e->v.Constant.value);
4648 39319 default:
4649 39319 return NULL;
4650 }
4651 }
4652
4653 static int
4654 1119690 check_caller(struct compiler *c, expr_ty e)
4655 {
4656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1119690 times.
1119690 switch (e->kind) {
4657 case Constant_kind:
4658 case Tuple_kind:
4659 case List_kind:
4660 case ListComp_kind:
4661 case Dict_kind:
4662 case DictComp_kind:
4663 case Set_kind:
4664 case SetComp_kind:
4665 case GeneratorExp_kind:
4666 case JoinedStr_kind:
4667 case FormattedValue_kind:
4668 return compiler_warn(c, "'%.200s' object is not callable; "
4669 "perhaps you missed a comma?",
4670 infer_type(e)->tp_name);
4671 1119690 default:
4672 1119690 return 1;
4673 }
4674 }
4675
4676 static int
4677 132329 check_subscripter(struct compiler *c, expr_ty e)
4678 {
4679 PyObject *v;
4680
4681
2/3
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 132249 times.
132329 switch (e->kind) {
4682 80 case Constant_kind:
4683 80 v = e->v.Constant.value;
4684
4/8
✓ Branch 0 taken 80 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 80 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 80 times.
✗ Branch 7 not taken.
240 if (!(v == Py_None || v == Py_Ellipsis ||
4685
2/4
✓ Branch 3 taken 80 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 80 times.
✗ Branch 7 not taken.
160 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4686
3/6
✓ Branch 2 taken 80 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 80 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 80 times.
✗ Branch 11 not taken.
160 PyAnySet_Check(v)))
4687 {
4688 80 return 1;
4689 }
4690 /* fall through */
4691 case Set_kind:
4692 case SetComp_kind:
4693 case GeneratorExp_kind:
4694 case Lambda_kind:
4695 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4696 "perhaps you missed a comma?",
4697 infer_type(e)->tp_name);
4698 132249 default:
4699 132249 return 1;
4700 }
4701 }
4702
4703 static int
4704 132329 check_index(struct compiler *c, expr_ty e, expr_ty s)
4705 {
4706 PyObject *v;
4707
4708 132329 PyTypeObject *index_type = infer_type(s);
4709
2/2
✓ Branch 0 taken 93010 times.
✓ Branch 1 taken 39319 times.
132329 if (index_type == NULL
4710
2/2
✓ Branch 1 taken 16790 times.
✓ Branch 2 taken 76220 times.
93010 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16790 times.
16790 || index_type == &PySlice_Type) {
4712 115539 return 1;
4713 }
4714
4715
1/3
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 16790 times.
16790 switch (e->kind) {
4716 case Constant_kind:
4717 v = e->v.Constant.value;
4718 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4719 return 1;
4720 }
4721 /* fall through */
4722 case Tuple_kind:
4723 case List_kind:
4724 case ListComp_kind:
4725 case JoinedStr_kind:
4726 case FormattedValue_kind:
4727 return compiler_warn(c, "%.200s indices must be integers or slices, "
4728 "not %.200s; "
4729 "perhaps you missed a comma?",
4730 infer_type(e)->tp_name,
4731 index_type->tp_name);
4732 16790 default:
4733 16790 return 1;
4734 }
4735 }
4736
4737 static int
4738 318261 is_import_originated(struct compiler *c, expr_ty e)
4739 {
4740 /* Check whether the global scope has an import named
4741 e, if it is a Name object. For not traversing all the
4742 scope stack every time this function is called, it will
4743 only check the global scope to determine whether something
4744 is imported or not. */
4745
4746
2/2
✓ Branch 0 taken 108453 times.
✓ Branch 1 taken 209808 times.
318261 if (e->kind != Name_kind) {
4747 108453 return 0;
4748 }
4749
4750 209808 long flags = _PyST_GetSymbol(c->c_st->st_top, e->v.Name.id);
4751 209808 return flags & DEF_IMPORT;
4752 }
4753
4754 static void
4755 515888 update_location_to_match_attr(struct compiler *c, expr_ty meth)
4756 {
4757
2/2
✓ Branch 0 taken 4056 times.
✓ Branch 1 taken 511832 times.
515888 if (meth->lineno != meth->end_lineno) {
4758 // Make start location match attribute
4759 4056 c->u->u_loc.lineno = meth->end_lineno;
4760 4056 c->u->u_loc.col_offset = meth->end_col_offset - (int)PyUnicode_GetLength(meth->v.Attribute.attr)-1;
4761 }
4762 515888 }
4763
4764 // Return 1 if the method call was optimized, -1 if not, and 0 on error.
4765 static int
4766 1377634 maybe_optimize_method_call(struct compiler *c, expr_ty e)
4767 {
4768 Py_ssize_t argsl, i, kwdsl;
4769 1377634 expr_ty meth = e->v.Call.func;
4770 1377634 asdl_expr_seq *args = e->v.Call.args;
4771 1377634 asdl_keyword_seq *kwds = e->v.Call.keywords;
4772
4773 /* Check that the call node is an attribute access */
4774
3/4
✓ Branch 0 taken 318261 times.
✓ Branch 1 taken 1059373 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 318261 times.
1377634 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load) {
4775 1059373 return -1;
4776 }
4777
4778 /* Check that the base object is not something that is imported */
4779
2/2
✓ Branch 1 taken 55162 times.
✓ Branch 2 taken 263099 times.
318261 if (is_import_originated(c, meth->v.Attribute.value)) {
4780 55162 return -1;
4781 }
4782
4783 /* Check that there aren't too many arguments */
4784
2/2
✓ Branch 0 taken 197175 times.
✓ Branch 1 taken 65924 times.
263099 argsl = asdl_seq_LEN(args);
4785
2/2
✓ Branch 0 taken 17382 times.
✓ Branch 1 taken 245717 times.
263099 kwdsl = asdl_seq_LEN(kwds);
4786
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 263098 times.
263099 if (argsl + kwdsl + (kwdsl != 0) >= STACK_USE_GUIDELINE) {
4787 1 return -1;
4788 }
4789 /* Check that there are no *varargs types of arguments. */
4790
2/2
✓ Branch 0 taken 295453 times.
✓ Branch 1 taken 260628 times.
556081 for (i = 0; i < argsl; i++) {
4791 295453 expr_ty elt = asdl_seq_GET(args, i);
4792
2/2
✓ Branch 0 taken 2470 times.
✓ Branch 1 taken 292983 times.
295453 if (elt->kind == Starred_kind) {
4793 2470 return -1;
4794 }
4795 }
4796
4797
2/2
✓ Branch 0 taken 26449 times.
✓ Branch 1 taken 257944 times.
284393 for (i = 0; i < kwdsl; i++) {
4798 26449 keyword_ty kw = asdl_seq_GET(kwds, i);
4799
2/2
✓ Branch 0 taken 2684 times.
✓ Branch 1 taken 23765 times.
26449 if (kw->arg == NULL) {
4800 2684 return -1;
4801 }
4802 }
4803 /* Alright, we can optimize the code. */
4804
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 257944 times.
257944 VISIT(c, expr, meth->v.Attribute.value);
4805 257944 SET_LOC(c, meth);
4806 257944 update_location_to_match_attr(c, meth);
4807
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 257944 times.
257944 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4808
5/6
✗ Branch 1 not taken.
✓ Branch 2 taken 289117 times.
✓ Branch 3 taken 482072 times.
✓ Branch 4 taken 64989 times.
✓ Branch 5 taken 289117 times.
✓ Branch 6 taken 257944 times.
547061 VISIT_SEQ(c, expr, e->v.Call.args);
4809
4810
2/2
✓ Branch 0 taken 13900 times.
✓ Branch 1 taken 244044 times.
257944 if (kwdsl) {
4811
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 22502 times.
✓ Branch 3 taken 36402 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 22502 times.
✓ Branch 6 taken 13900 times.
36402 VISIT_SEQ(c, keyword, kwds);
4812
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13900 times.
13900 if (!compiler_call_simple_kw_helper(c, kwds, kwdsl)) {
4813 return 0;
4814 };
4815 }
4816 257944 SET_LOC(c, e);
4817 257944 update_location_to_match_attr(c, meth);
4818
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 257944 times.
257944 ADDOP_I(c, CALL, argsl + kwdsl);
4819 257944 return 1;
4820 }
4821
4822 static int
4823 2512528 validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
4824 {
4825
2/2
✓ Branch 0 taken 113920 times.
✓ Branch 1 taken 2398608 times.
2512528 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4826
2/2
✓ Branch 0 taken 211266 times.
✓ Branch 1 taken 2512528 times.
2723794 for (Py_ssize_t i = 0; i < nkeywords; i++) {
4827 211266 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4828
2/2
✓ Branch 0 taken 19876 times.
✓ Branch 1 taken 191390 times.
211266 if (key->arg == NULL) {
4829 19876 continue;
4830 }
4831
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 191390 times.
191390 if (forbidden_name(c, key->arg, Store)) {
4832 return -1;
4833 }
4834
2/2
✓ Branch 0 taken 300305 times.
✓ Branch 1 taken 191390 times.
491695 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
4835 300305 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4836
3/4
✓ Branch 0 taken 288775 times.
✓ Branch 1 taken 11530 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 288775 times.
300305 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
4837 SET_LOC(c, other);
4838 compiler_error(c, "keyword argument repeated: %U", key->arg);
4839 return -1;
4840 }
4841 }
4842 }
4843 2512528 return 0;
4844 }
4845
4846 static int
4847 1377634 compiler_call(struct compiler *c, expr_ty e)
4848 {
4849
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1377634 times.
1377634 if (validate_keywords(c, e->v.Call.keywords) == -1) {
4850 return 0;
4851 }
4852 1377634 int ret = maybe_optimize_method_call(c, e);
4853
2/2
✓ Branch 0 taken 257944 times.
✓ Branch 1 taken 1119690 times.
1377634 if (ret >= 0) {
4854 257944 return ret;
4855 }
4856
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1119690 times.
1119690 if (!check_caller(c, e->v.Call.func)) {
4857 return 0;
4858 }
4859 1119690 SET_LOC(c, e->v.Call.func);
4860
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1119690 times.
1119690 ADDOP(c, PUSH_NULL);
4861 1119690 SET_LOC(c, e);
4862
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1119690 times.
1119690 VISIT(c, expr, e->v.Call.func);
4863 1119690 return compiler_call_helper(c, 0,
4864 e->v.Call.args,
4865 e->v.Call.keywords);
4866 }
4867
4868 static int
4869 10984 compiler_joined_str(struct compiler *c, expr_ty e)
4870 {
4871
4872
1/2
✓ Branch 0 taken 10984 times.
✗ Branch 1 not taken.
10984 Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
4873
2/2
✓ Branch 0 taken 233 times.
✓ Branch 1 taken 10751 times.
10984 if (value_count > STACK_USE_GUIDELINE) {
4874 _Py_DECLARE_STR(empty, "");
4875
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 233 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 233 times.
233 ADDOP_LOAD_CONST_NEW(c, &_Py_STR(empty));
4876
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
233 ADDOP_NAME(c, LOAD_METHOD, &_Py_ID(join), names);
4877
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
233 ADDOP_I(c, BUILD_LIST, 0);
4878
3/4
✓ Branch 0 taken 7456 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7223 times.
✓ Branch 3 taken 233 times.
7456 for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
4879
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7223 times.
7223 VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
4880
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7223 times.
7223 ADDOP_I(c, LIST_APPEND, 1);
4881 }
4882
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 233 times.
233 ADDOP_I(c, CALL, 1);
4883 }
4884 else {
4885
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 64840 times.
✓ Branch 3 taken 75591 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 64840 times.
✓ Branch 6 taken 10751 times.
75591 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
4886
3/4
✓ Branch 0 taken 10751 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10619 times.
✓ Branch 3 taken 132 times.
10751 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) {
4887
2/4
✓ Branch 0 taken 10619 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 10619 times.
10619 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
4888 }
4889 }
4890 10984 return 1;
4891 }
4892
4893 /* Used to implement f-strings. Format a single value. */
4894 static int
4895 33769 compiler_formatted_value(struct compiler *c, expr_ty e)
4896 {
4897 /* Our oparg encodes 2 pieces of information: the conversion
4898 character, and whether or not a format_spec was provided.
4899
4900 Convert the conversion char to 3 bits:
4901 : 000 0x0 FVC_NONE The default if nothing specified.
4902 !s : 001 0x1 FVC_STR
4903 !r : 010 0x2 FVC_REPR
4904 !a : 011 0x3 FVC_ASCII
4905
4906 next bit is whether or not we have a format spec:
4907 yes : 100 0x4
4908 no : 000 0x0
4909 */
4910
4911 33769 int conversion = e->v.FormattedValue.conversion;
4912 int oparg;
4913
4914 /* The expression to be formatted. */
4915
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33769 times.
33769 VISIT(c, expr, e->v.FormattedValue.value);
4916
4917
4/5
✓ Branch 0 taken 10309 times.
✓ Branch 1 taken 20279 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 3180 times.
✗ Branch 4 not taken.
33769 switch (conversion) {
4918 10309 case 's': oparg = FVC_STR; break;
4919 20279 case 'r': oparg = FVC_REPR; break;
4920 1 case 'a': oparg = FVC_ASCII; break;
4921 3180 case -1: oparg = FVC_NONE; break;
4922 default:
4923 PyErr_Format(PyExc_SystemError,
4924 "Unrecognized conversion character %d", conversion);
4925 return 0;
4926 }
4927
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 33692 times.
33769 if (e->v.FormattedValue.format_spec) {
4928 /* Evaluate the format spec, and update our opcode arg. */
4929
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
77 VISIT(c, expr, e->v.FormattedValue.format_spec);
4930 77 oparg |= FVS_HAVE_SPEC;
4931 }
4932
4933 /* And push our opcode and oparg */
4934
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33769 times.
33769 ADDOP_I(c, FORMAT_VALUE, oparg);
4935
4936 33769 return 1;
4937 }
4938
4939 static int
4940 2448 compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
4941 {
4942 2448 Py_ssize_t i, n = end - begin;
4943 keyword_ty kw;
4944 PyObject *keys, *key;
4945 assert(n > 0);
4946 2448 int big = n*2 > STACK_USE_GUIDELINE;
4947
4/4
✓ Branch 0 taken 1102 times.
✓ Branch 1 taken 1346 times.
✓ Branch 2 taken 955 times.
✓ Branch 3 taken 147 times.
2448 if (n > 1 && !big) {
4948
2/2
✓ Branch 0 taken 4704 times.
✓ Branch 1 taken 955 times.
5659 for (i = begin; i < end; i++) {
4949 4704 kw = asdl_seq_GET(keywords, i);
4950
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4704 times.
4704 VISIT(c, expr, kw->value);
4951 }
4952 955 keys = PyTuple_New(n);
4953
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 955 times.
955 if (keys == NULL) {
4954 return 0;
4955 }
4956
2/2
✓ Branch 0 taken 4704 times.
✓ Branch 1 taken 955 times.
5659 for (i = begin; i < end; i++) {
4957 4704 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4958 4704 Py_INCREF(key);
4959 4704 PyTuple_SET_ITEM(keys, i - begin, key);
4960 }
4961
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 955 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 955 times.
955 ADDOP_LOAD_CONST_NEW(c, keys);
4962
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 955 times.
955 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4963 955 return 1;
4964 }
4965
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 1346 times.
1493 if (big) {
4966
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 147 times.
147 ADDOP_I_NOLINE(c, BUILD_MAP, 0);
4967 }
4968
2/2
✓ Branch 0 taken 5501 times.
✓ Branch 1 taken 1493 times.
6994 for (i = begin; i < end; i++) {
4969 5501 kw = asdl_seq_GET(keywords, i);
4970
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5501 times.
5501 ADDOP_LOAD_CONST(c, kw->arg);
4971
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5501 times.
5501 VISIT(c, expr, kw->value);
4972
2/2
✓ Branch 0 taken 4155 times.
✓ Branch 1 taken 1346 times.
5501 if (big) {
4973
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4155 times.
4155 ADDOP_I_NOLINE(c, MAP_ADD, 1);
4974 }
4975 }
4976
2/2
✓ Branch 0 taken 1346 times.
✓ Branch 1 taken 147 times.
1493 if (!big) {
4977
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1346 times.
1346 ADDOP_I(c, BUILD_MAP, n);
4978 }
4979 1493 return 1;
4980 }
4981
4982 /* Used by compiler_call_helper and maybe_optimize_method_call to emit
4983 * KW_NAMES before CALL.
4984 * Returns 1 on success, 0 on error.
4985 */
4986 static int
4987 53704 compiler_call_simple_kw_helper(struct compiler *c,
4988 asdl_keyword_seq *keywords,
4989 Py_ssize_t nkwelts)
4990 {
4991 PyObject *names;
4992 53704 names = PyTuple_New(nkwelts);
4993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53704 times.
53704 if (names == NULL) {
4994 return 0;
4995 }
4996
2/2
✓ Branch 0 taken 96859 times.
✓ Branch 1 taken 53704 times.
150563 for (int i = 0; i < nkwelts; i++) {
4997 96859 keyword_ty kw = asdl_seq_GET(keywords, i);
4998 96859 Py_INCREF(kw->arg);
4999 96859 PyTuple_SET_ITEM(names, i, kw->arg);
5000 }
5001 53704 Py_ssize_t arg = compiler_add_const(c, names);
5002
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53704 times.
53704 if (arg < 0) {
5003 return 0;
5004 }
5005 53704 Py_DECREF(names);
5006
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 53704 times.
53704 ADDOP_I(c, KW_NAMES, arg);
5007 53704 return 1;
5008 }
5009
5010
5011 /* shared code between compiler_call and compiler_class */
5012 static int
5013 1134894 compiler_call_helper(struct compiler *c,
5014 int n, /* Args already pushed */
5015 asdl_expr_seq *args,
5016 asdl_keyword_seq *keywords)
5017 {
5018 Py_ssize_t i, nseen, nelts, nkwelts;
5019
5020
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1134894 times.
1134894 if (validate_keywords(c, keywords) == -1) {
5021 return 0;
5022 }
5023
5024
2/2
✓ Branch 0 taken 1094575 times.
✓ Branch 1 taken 40319 times.
1134894 nelts = asdl_seq_LEN(args);
5025
2/2
✓ Branch 0 taken 50121 times.
✓ Branch 1 taken 1084773 times.
1134894 nkwelts = asdl_seq_LEN(keywords);
5026
5027
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 1134741 times.
1134894 if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
5028 153 goto ex_call;
5029 }
5030
2/2
✓ Branch 0 taken 1648598 times.
✓ Branch 1 taken 1123857 times.
2772455 for (i = 0; i < nelts; i++) {
5031 1648598 expr_ty elt = asdl_seq_GET(args, i);
5032
2/2
✓ Branch 0 taken 10884 times.
✓ Branch 1 taken 1637714 times.
1648598 if (elt->kind == Starred_kind) {
5033 10884 goto ex_call;
5034 }
5035 }
5036
2/2
✓ Branch 0 taken 86545 times.
✓ Branch 1 taken 1117331 times.
1203876 for (i = 0; i < nkwelts; i++) {
5037 86545 keyword_ty kw = asdl_seq_GET(keywords, i);
5038
2/2
✓ Branch 0 taken 6526 times.
✓ Branch 1 taken 80019 times.
86545 if (kw->arg == NULL) {
5039 6526 goto ex_call;
5040 }
5041 }
5042
5043 /* No * or ** args, so can use faster calling sequence */
5044
2/2
✓ Branch 0 taken 1619706 times.
✓ Branch 1 taken 1117331 times.
2737037 for (i = 0; i < nelts; i++) {
5045 1619706 expr_ty elt = asdl_seq_GET(args, i);
5046 assert(elt->kind != Starred_kind);
5047
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1619706 times.
1619706 VISIT(c, expr, elt);
5048 }
5049
2/2
✓ Branch 0 taken 39804 times.
✓ Branch 1 taken 1077527 times.
1117331 if (nkwelts) {
5050
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 74357 times.
✓ Branch 3 taken 114161 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 74357 times.
✓ Branch 6 taken 39804 times.
114161 VISIT_SEQ(c, keyword, keywords);
5051
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39804 times.
39804 if (!compiler_call_simple_kw_helper(c, keywords, nkwelts)) {
5052 return 0;
5053 };
5054 }
5055
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1117331 times.
1117331 ADDOP_I(c, CALL, n + nelts + nkwelts);
5056 1117331 return 1;
5057
5058 17563 ex_call:
5059
5060 /* Do positional arguments. */
5061
5/6
✓ Branch 0 taken 17563 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8550 times.
✓ Branch 3 taken 9013 times.
✓ Branch 4 taken 6067 times.
✓ Branch 5 taken 2483 times.
17563 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
5062
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6067 times.
6067 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
5063 }
5064
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11496 times.
11496 else if (starunpack_helper(c, args, n, BUILD_LIST,
5065 LIST_APPEND, LIST_EXTEND, 1) == 0) {
5066 return 0;
5067 }
5068 /* Then keyword arguments */
5069
2/2
✓ Branch 0 taken 10317 times.
✓ Branch 1 taken 7246 times.
17563 if (nkwelts) {
5070 /* Has a new dict been pushed */
5071 10317 int have_dict = 0;
5072
5073 10317 nseen = 0; /* the number of keyword arguments on the stack following */
5074
2/2
✓ Branch 0 taken 20143 times.
✓ Branch 1 taken 10317 times.
30460 for (i = 0; i < nkwelts; i++) {
5075 20143 keyword_ty kw = asdl_seq_GET(keywords, i);
5076
2/2
✓ Branch 0 taken 9938 times.
✓ Branch 1 taken 10205 times.
20143 if (kw->arg == NULL) {
5077 /* A keyword argument unpacking. */
5078
2/2
✓ Branch 0 taken 2068 times.
✓ Branch 1 taken 7870 times.
9938 if (nseen) {
5079
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2068 times.
2068 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
5080 return 0;
5081 }
5082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2068 times.
2068 if (have_dict) {
5083 ADDOP_I(c, DICT_MERGE, 1);
5084 }
5085 2068 have_dict = 1;
5086 2068 nseen = 0;
5087 }
5088
2/2
✓ Branch 0 taken 7869 times.
✓ Branch 1 taken 2069 times.
9938 if (!have_dict) {
5089
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7869 times.
7869 ADDOP_I(c, BUILD_MAP, 0);
5090 7869 have_dict = 1;
5091 }
5092
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9938 times.
9938 VISIT(c, expr, kw->value);
5093
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9938 times.
9938 ADDOP_I(c, DICT_MERGE, 1);
5094 }
5095 else {
5096 10205 nseen++;
5097 }
5098 }
5099
2/2
✓ Branch 0 taken 380 times.
✓ Branch 1 taken 9937 times.
10317 if (nseen) {
5100 /* Pack up any trailing keyword arguments. */
5101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 380 times.
380 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
5102 return 0;
5103 }
5104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 380 times.
380 if (have_dict) {
5105 ADDOP_I(c, DICT_MERGE, 1);
5106 }
5107 380 have_dict = 1;
5108 }
5109 assert(have_dict);
5110 }
5111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17563 times.
17563 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
5112 17563 return 1;
5113 }
5114
5115
5116 /* List and set comprehensions and generator expressions work by creating a
5117 nested function to perform the actual iteration. This means that the
5118 iteration variables don't leak into the current scope.
5119 The defined function is called immediately following its definition, with the
5120 result of that call being the result of the expression.
5121 The LC/SC version returns the populated container, while the GE version is
5122 flagged in symtable.c as a generator, so it returns the generator object
5123 when the function is called.
5124
5125 Possible cleanups:
5126 - iterate over the generator sequence instead of using recursion
5127 */
5128
5129
5130 static int
5131 16504 compiler_comprehension_generator(struct compiler *c,
5132 asdl_comprehension_seq *generators, int gen_index,
5133 int depth,
5134 expr_ty elt, expr_ty val, int type)
5135 {
5136 comprehension_ty gen;
5137 16504 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
5138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16504 times.
16504 if (gen->is_async) {
5139 return compiler_async_comprehension_generator(
5140 c, generators, gen_index, depth, elt, val, type);
5141 } else {
5142 16504 return compiler_sync_comprehension_generator(
5143 c, generators, gen_index, depth, elt, val, type);
5144 }
5145 }
5146
5147 static int
5148 16504 compiler_sync_comprehension_generator(struct compiler *c,
5149 asdl_comprehension_seq *generators, int gen_index,
5150 int depth,
5151 expr_ty elt, expr_ty val, int type)
5152 {
5153 /* generate code for the iterator, then each of the ifs,
5154 and then write to the element */
5155
5156 comprehension_ty gen;
5157 basicblock *start, *anchor, *if_cleanup;
5158 Py_ssize_t i, n;
5159
5160 16504 start = compiler_new_block(c);
5161 16504 if_cleanup = compiler_new_block(c);
5162 16504 anchor = compiler_new_block(c);
5163
5164
3/6
✓ Branch 0 taken 16504 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16504 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 16504 times.
16504 if (start == NULL || if_cleanup == NULL || anchor == NULL) {
5165 return 0;
5166 }
5167
5168 16504 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
5169
5170
2/2
✓ Branch 0 taken 16005 times.
✓ Branch 1 taken 499 times.
16504 if (gen_index == 0) {
5171 /* Receive outermost iter as an implicit argument */
5172 16005 c->u->u_argcount = 1;
5173
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16005 times.
16005 ADDOP_I(c, LOAD_FAST, 0);
5174 }
5175 else {
5176 /* Sub-iter - calculate on the fly */
5177 /* Fast path for the temporary variable assignment idiom:
5178 for y in [f(x)]
5179 */
5180 asdl_expr_seq *elts;
5181
2/3
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 486 times.
499 switch (gen->iter->kind) {
5182 case List_kind:
5183 elts = gen->iter->v.List.elts;
5184 break;
5185 13 case Tuple_kind:
5186 13 elts = gen->iter->v.Tuple.elts;
5187 13 break;
5188 486 default:
5189 486 elts = NULL;
5190 }
5191
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 486 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
499 if (asdl_seq_LEN(elts) == 1) {
5192 expr_ty elt = asdl_seq_GET(elts, 0);
5193 if (elt->kind != Starred_kind) {
5194 VISIT(c, expr, elt);
5195 start = NULL;
5196 }
5197 }
5198
1/2
✓ Branch 0 taken 499 times.
✗ Branch 1 not taken.
499 if (start) {
5199
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 499 times.
499 VISIT(c, expr, gen->iter);
5200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 499 times.
499 ADDOP(c, GET_ITER);
5201 }
5202 }
5203
1/2
✓ Branch 0 taken 16504 times.
✗ Branch 1 not taken.
16504 if (start) {
5204 16504 depth++;
5205 16504 compiler_use_next_block(c, start);
5206
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16504 times.
16504 ADDOP_JUMP(c, FOR_ITER, anchor);
5207 }
5208
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16504 times.
16504 VISIT(c, expr, gen->target);
5209
5210 /* XXX this needs to be cleaned up...a lot! */
5211
1/2
✓ Branch 0 taken 16504 times.
✗ Branch 1 not taken.
16504 n = asdl_seq_LEN(gen->ifs);
5212
2/2
✓ Branch 0 taken 2314 times.
✓ Branch 1 taken 16504 times.
18818 for (i = 0; i < n; i++) {
5213 2314 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
5214
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2314 times.
2314 if (!compiler_jump_if(c, e, if_cleanup, 0))
5215 return 0;
5216 }
5217
5218
3/4
✓ Branch 0 taken 16504 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 499 times.
✓ Branch 3 taken 16005 times.
16504 if (++gen_index < asdl_seq_LEN(generators))
5219
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 499 times.
499 if (!compiler_comprehension_generator(c,
5220 generators, gen_index, depth,
5221 elt, val, type))
5222 return 0;
5223
5224 /* only append after the last for generator */
5225
3/4
✓ Branch 0 taken 16504 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16005 times.
✓ Branch 3 taken 499 times.
16504 if (gen_index >= asdl_seq_LEN(generators)) {
5226 /* comprehension specific code */
5227
4/5
✓ Branch 0 taken 5547 times.
✓ Branch 1 taken 9279 times.
✓ Branch 2 taken 351 times.
✓ Branch 3 taken 828 times.
✗ Branch 4 not taken.
16005 switch (type) {
5228 5547 case COMP_GENEXP:
5229
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5547 times.
5547 VISIT(c, expr, elt);
5230
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5547 times.
5547 ADDOP_YIELD(c);
5231
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5547 times.
5547 ADDOP(c, POP_TOP);
5232 5547 break;
5233 9279 case COMP_LISTCOMP:
5234
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9279 times.
9279 VISIT(c, expr, elt);
5235
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9279 times.
9279 ADDOP_I(c, LIST_APPEND, depth + 1);
5236 9279 break;
5237 351 case COMP_SETCOMP:
5238
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 351 times.
351 VISIT(c, expr, elt);
5239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 351 times.
351 ADDOP_I(c, SET_ADD, depth + 1);
5240 351 break;
5241 828 case COMP_DICTCOMP:
5242 /* With '{k: v}', k is evaluated before v, so we do
5243 the same. */
5244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 828 times.
828 VISIT(c, expr, elt);
5245
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 828 times.
828 VISIT(c, expr, val);
5246
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 828 times.
828 ADDOP_I(c, MAP_ADD, depth + 1);
5247 828 break;
5248 default:
5249 return 0;
5250 }
5251 }
5252 16504 compiler_use_next_block(c, if_cleanup);
5253
1/2
✓ Branch 0 taken 16504 times.
✗ Branch 1 not taken.
16504 if (start) {
5254
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16504 times.
16504 ADDOP_JUMP(c, JUMP, start);
5255 16504 compiler_use_next_block(c, anchor);
5256 }
5257
5258 16504 return 1;
5259 }
5260
5261 static int
5262 compiler_async_comprehension_generator(struct compiler *c,
5263 asdl_comprehension_seq *generators, int gen_index,
5264 int depth,
5265 expr_ty elt, expr_ty val, int type)
5266 {
5267 comprehension_ty gen;
5268 basicblock *start, *if_cleanup, *except;
5269 Py_ssize_t i, n;
5270 start = compiler_new_block(c);
5271 except = compiler_new_block(c);
5272 if_cleanup = compiler_new_block(c);
5273
5274 if (start == NULL || if_cleanup == NULL || except == NULL) {
5275 return 0;
5276 }
5277
5278 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
5279
5280 if (gen_index == 0) {
5281 /* Receive outermost iter as an implicit argument */
5282 c->u->u_argcount = 1;
5283 ADDOP_I(c, LOAD_FAST, 0);
5284 }
5285 else {
5286 /* Sub-iter - calculate on the fly */
5287 VISIT(c, expr, gen->iter);
5288 ADDOP(c, GET_AITER);
5289 }
5290
5291 compiler_use_next_block(c, start);
5292 /* Runtime will push a block here, so we need to account for that */
5293 if (!compiler_push_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start,
5294 NULL, NULL)) {
5295 return 0;
5296 }
5297
5298 ADDOP_JUMP(c, SETUP_FINALLY, except);
5299 ADDOP(c, GET_ANEXT);
5300 ADDOP_LOAD_CONST(c, Py_None);
5301 ADD_YIELD_FROM(c, 1);
5302 ADDOP(c, POP_BLOCK);
5303 VISIT(c, expr, gen->target);
5304
5305 n = asdl_seq_LEN(gen->ifs);
5306 for (i = 0; i < n; i++) {
5307 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
5308 if (!compiler_jump_if(c, e, if_cleanup, 0))
5309 return 0;
5310 }
5311
5312 depth++;
5313 if (++gen_index < asdl_seq_LEN(generators))
5314 if (!compiler_comprehension_generator(c,
5315 generators, gen_index, depth,
5316 elt, val, type))
5317 return 0;
5318
5319 /* only append after the last for generator */
5320 if (gen_index >= asdl_seq_LEN(generators)) {
5321 /* comprehension specific code */
5322 switch (type) {
5323 case COMP_GENEXP:
5324 VISIT(c, expr, elt);
5325 ADDOP_YIELD(c);
5326 ADDOP(c, POP_TOP);
5327 break;
5328 case COMP_LISTCOMP:
5329 VISIT(c, expr, elt);
5330 ADDOP_I(c, LIST_APPEND, depth + 1);
5331 break;
5332 case COMP_SETCOMP:
5333 VISIT(c, expr, elt);
5334 ADDOP_I(c, SET_ADD, depth + 1);
5335 break;
5336 case COMP_DICTCOMP:
5337 /* With '{k: v}', k is evaluated before v, so we do
5338 the same. */
5339 VISIT(c, expr, elt);
5340 VISIT(c, expr, val);
5341 ADDOP_I(c, MAP_ADD, depth + 1);
5342 break;
5343 default:
5344 return 0;
5345 }
5346 }
5347 compiler_use_next_block(c, if_cleanup);
5348 ADDOP_JUMP(c, JUMP, start);
5349
5350 compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
5351
5352 compiler_use_next_block(c, except);
5353 //UNSET_LOC(c);
5354
5355 ADDOP(c, END_ASYNC_FOR);
5356
5357 return 1;
5358 }
5359
5360 static int
5361 16005 compiler_comprehension(struct compiler *c, expr_ty e, int type,
5362 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
5363 expr_ty val)
5364 {
5365 16005 PyCodeObject *co = NULL;
5366 comprehension_ty outermost;
5367 16005 PyObject *qualname = NULL;
5368 16005 int scope_type = c->u->u_scope_type;
5369 16005 int is_async_generator = 0;
5370
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16005 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16005 int is_top_level_await = IS_TOP_LEVEL_AWAIT(c);
5371
5372 16005 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
5373
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16005 times.
16005 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
5374 (void *)e, e->lineno))
5375 {
5376 goto error;
5377 }
5378 16005 SET_LOC(c, e);
5379
5380 16005 is_async_generator = c->u->u_ste->ste_coroutine;
5381
5382
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 16005 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
16005 if (is_async_generator && type != COMP_GENEXP &&
5383 scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
5384 scope_type != COMPILER_SCOPE_COMPREHENSION &&
5385 !is_top_level_await)
5386 {
5387 compiler_error(c, "asynchronous comprehension outside of "
5388 "an asynchronous function");
5389 goto error_in_scope;
5390 }
5391
5392
2/2
✓ Branch 0 taken 10458 times.
✓ Branch 1 taken 5547 times.
16005 if (type != COMP_GENEXP) {
5393 int op;
5394
3/4
✓ Branch 0 taken 9279 times.
✓ Branch 1 taken 351 times.
✓ Branch 2 taken 828 times.
✗ Branch 3 not taken.
10458 switch (type) {
5395 9279 case COMP_LISTCOMP:
5396 9279 op = BUILD_LIST;
5397 9279 break;
5398 351 case COMP_SETCOMP:
5399 351 op = BUILD_SET;
5400 351 break;
5401 828 case COMP_DICTCOMP:
5402 828 op = BUILD_MAP;
5403 828 break;
5404 default:
5405 PyErr_Format(PyExc_SystemError,
5406 "unknown comprehension type %d", type);
5407 goto error_in_scope;
5408 }
5409
5410
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10458 times.
10458 ADDOP_I(c, op, 0);
5411 }
5412
5413
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16005 times.
16005 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
5414 val, type))
5415 goto error_in_scope;
5416
5417
2/2
✓ Branch 0 taken 10458 times.
✓ Branch 1 taken 5547 times.
16005 if (type != COMP_GENEXP) {
5418
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10458 times.
10458 ADDOP(c, RETURN_VALUE);
5419 }
5420
5421 16005 co = assemble(c, 1);
5422 16005 qualname = c->u->u_qualname;
5423 16005 Py_INCREF(qualname);
5424 16005 compiler_exit_scope(c);
5425
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16005 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16005 if (is_top_level_await && is_async_generator){
5426 c->u->u_ste->ste_coroutine = 1;
5427 }
5428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16005 times.
16005 if (co == NULL)
5429 goto error;
5430
5431
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16005 times.
16005 if (!compiler_make_closure(c, co, 0, qualname)) {
5432 goto error;
5433 }
5434 16005 Py_DECREF(qualname);
5435 16005 Py_DECREF(co);
5436
5437
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16005 times.
16005 VISIT(c, expr, outermost->iter);
5438
5439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16005 times.
16005 if (outermost->is_async) {
5440 ADDOP(c, GET_AITER);
5441 } else {
5442
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16005 times.
16005 ADDOP(c, GET_ITER);
5443 }
5444
5445
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16005 times.
16005 ADDOP_I(c, CALL, 0);
5446
5447
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 16005 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
16005 if (is_async_generator && type != COMP_GENEXP) {
5448 ADDOP_I(c, GET_AWAITABLE, 0);
5449 ADDOP_LOAD_CONST(c, Py_None);
5450 ADD_YIELD_FROM(c, 1);
5451 }
5452
5453 16005 return 1;
5454 error_in_scope:
5455 compiler_exit_scope(c);
5456 error:
5457 Py_XDECREF(qualname);
5458 Py_XDECREF(co);
5459 return 0;
5460 }
5461
5462 static int
5463 5547 compiler_genexp(struct compiler *c, expr_ty e)
5464 {
5465 assert(e->kind == GeneratorExp_kind);
5466 _Py_DECLARE_STR(anon_genexpr, "<genexpr>");
5467 5547 return compiler_comprehension(c, e, COMP_GENEXP, &_Py_STR(anon_genexpr),
5468 e->v.GeneratorExp.generators,
5469 e->v.GeneratorExp.elt, NULL);
5470 }
5471
5472 static int
5473 9279 compiler_listcomp(struct compiler *c, expr_ty e)
5474 {
5475 assert(e->kind == ListComp_kind);
5476 _Py_DECLARE_STR(anon_listcomp, "<listcomp>");
5477 9279 return compiler_comprehension(c, e, COMP_LISTCOMP, &_Py_STR(anon_listcomp),
5478 e->v.ListComp.generators,
5479 e->v.ListComp.elt, NULL);
5480 }
5481
5482 static int
5483 351 compiler_setcomp(struct compiler *c, expr_ty e)
5484 {
5485 assert(e->kind == SetComp_kind);
5486 _Py_DECLARE_STR(anon_setcomp, "<setcomp>");
5487 351 return compiler_comprehension(c, e, COMP_SETCOMP, &_Py_STR(anon_setcomp),
5488 e->v.SetComp.generators,
5489 e->v.SetComp.elt, NULL);
5490 }
5491
5492
5493 static int
5494 828 compiler_dictcomp(struct compiler *c, expr_ty e)
5495 {
5496 assert(e->kind == DictComp_kind);
5497 _Py_DECLARE_STR(anon_dictcomp, "<dictcomp>");
5498 828 return compiler_comprehension(c, e, COMP_DICTCOMP, &_Py_STR(anon_dictcomp),
5499 e->v.DictComp.generators,
5500 e->v.DictComp.key, e->v.DictComp.value);
5501 }
5502
5503
5504 static int
5505 96859 compiler_visit_keyword(struct compiler *c, keyword_ty k)
5506 {
5507
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 96859 times.
96859 VISIT(c, expr, k->value);
5508 96859 return 1;
5509 }
5510
5511
5512 static int
5513 4362 compiler_with_except_finish(struct compiler *c, basicblock * cleanup) {
5514 4362 UNSET_LOC(c);
5515 4362 basicblock *suppress = compiler_new_block(c);
5516
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4362 times.
4362 if (suppress == NULL) {
5517 return 0;
5518 }
5519
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4362 times.
4362 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, suppress);
5520
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4362 times.
4362 ADDOP_I(c, RERAISE, 2);
5521 4362 compiler_use_next_block(c, suppress);
5522
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4362 times.
4362 ADDOP(c, POP_TOP); /* exc_value */
5523
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4362 times.
4362 ADDOP(c, POP_BLOCK);
5524
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4362 times.
4362 ADDOP(c, POP_EXCEPT);
5525
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4362 times.
4362 ADDOP(c, POP_TOP);
5526
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4362 times.
4362 ADDOP(c, POP_TOP);
5527 4362 basicblock *exit = compiler_new_block(c);
5528
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4362 times.
4362 if (exit == NULL) {
5529 return 0;
5530 }
5531
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4362 times.
4362 ADDOP_JUMP(c, JUMP, exit);
5532 4362 compiler_use_next_block(c, cleanup);
5533
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4362 times.
4362 POP_EXCEPT_AND_RERAISE(c);
5534 4362 compiler_use_next_block(c, exit);
5535 4362 return 1;
5536 }
5537
5538 /*
5539 Implements the async with statement.
5540
5541 The semantics outlined in that PEP are as follows:
5542
5543 async with EXPR as VAR:
5544 BLOCK
5545
5546 It is implemented roughly as:
5547
5548 context = EXPR
5549 exit = context.__aexit__ # not calling it
5550 value = await context.__aenter__()
5551 try:
5552 VAR = value # if VAR present in the syntax
5553 BLOCK
5554 finally:
5555 if an exception was raised:
5556 exc = copy of (exception, instance, traceback)
5557 else:
5558 exc = (None, None, None)
5559 if not (await exit(*exc)):
5560 raise
5561 */
5562 static int
5563 14 compiler_async_with(struct compiler *c, stmt_ty s, int pos)
5564 {
5565 basicblock *block, *final, *exit, *cleanup;
5566 14 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
5567
5568 assert(s->kind == AsyncWith_kind);
5569
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
14 if (IS_TOP_LEVEL_AWAIT(c)){
5570 c->u->u_ste->ste_coroutine = 1;
5571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
5572 return compiler_error(c, "'async with' outside async function");
5573 }
5574
5575 14 block = compiler_new_block(c);
5576 14 final = compiler_new_block(c);
5577 14 exit = compiler_new_block(c);
5578 14 cleanup = compiler_new_block(c);
5579
4/8
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 14 times.
14 if (!block || !final || !exit || !cleanup)
5580 return 0;
5581
5582 /* Evaluate EXPR */
5583
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 VISIT(c, expr, item->context_expr);
5584
5585
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP(c, BEFORE_ASYNC_WITH);
5586
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP_I(c, GET_AWAITABLE, 1);
5587
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP_LOAD_CONST(c, Py_None);
5588
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADD_YIELD_FROM(c, 1);
5589
5590
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP_JUMP(c, SETUP_WITH, final);
5591
5592 /* SETUP_WITH pushes a finally block. */
5593 14 compiler_use_next_block(c, block);
5594
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, s)) {
5595 return 0;
5596 }
5597
5598
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11 times.
14 if (item->optional_vars) {
5599
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 VISIT(c, expr, item->optional_vars);
5600 }
5601 else {
5602 /* Discard result from context.__aenter__() */
5603
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 ADDOP(c, POP_TOP);
5604 }
5605
5606 14 pos++;
5607
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
5608 /* BLOCK code */
5609
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 47 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 33 times.
✓ Branch 6 taken 14 times.
47 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
5610 else if (!compiler_async_with(c, s, pos))
5611 return 0;
5612
5613 14 compiler_pop_fblock(c, ASYNC_WITH, block);
5614
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP(c, POP_BLOCK);
5615 /* End of body; start the cleanup */
5616
5617 /* For successful outcome:
5618 * call __exit__(None, None, None)
5619 */
5620 14 SET_LOC(c, s);
5621
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if(!compiler_call_exit_with_nones(c))
5622 return 0;
5623
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP_I(c, GET_AWAITABLE, 2);
5624
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP_LOAD_CONST(c, Py_None);
5625
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADD_YIELD_FROM(c, 1);
5626
5627
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP(c, POP_TOP);
5628
5629
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP_JUMP(c, JUMP, exit);
5630
5631 /* For exceptional outcome: */
5632 14 compiler_use_next_block(c, final);
5633
5634
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
5635
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP(c, PUSH_EXC_INFO);
5636
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP(c, WITH_EXCEPT_START);
5637
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP_I(c, GET_AWAITABLE, 2);
5638
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADDOP_LOAD_CONST(c, Py_None);
5639
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 ADD_YIELD_FROM(c, 1);
5640 14 compiler_with_except_finish(c, cleanup);
5641
5642 14 compiler_use_next_block(c, exit);
5643 14 return 1;
5644 }
5645
5646
5647 /*
5648 Implements the with statement from PEP 343.
5649 with EXPR as VAR:
5650 BLOCK
5651 is implemented as:
5652 <code for EXPR>
5653 SETUP_WITH E
5654 <code to store to VAR> or POP_TOP
5655 <code for BLOCK>
5656 LOAD_CONST (None, None, None)
5657 CALL_FUNCTION_EX 0
5658 JUMP EXIT
5659 E: WITH_EXCEPT_START (calls EXPR.__exit__)
5660 POP_JUMP_IF_TRUE T:
5661 RERAISE
5662 T: POP_TOP (remove exception from stack)
5663 POP_EXCEPT
5664 POP_TOP
5665 EXIT:
5666 */
5667
5668 static int
5669 4348 compiler_with(struct compiler *c, stmt_ty s, int pos)
5670 {
5671 basicblock *block, *final, *exit, *cleanup;
5672 4348 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
5673
5674 assert(s->kind == With_kind);
5675
5676 4348 block = compiler_new_block(c);
5677 4348 final = compiler_new_block(c);
5678 4348 exit = compiler_new_block(c);
5679 4348 cleanup = compiler_new_block(c);
5680
4/8
✓ Branch 0 taken 4348 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4348 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4348 times.
4348 if (!block || !final || !exit || !cleanup)
5681 return 0;
5682
5683 /* Evaluate EXPR */
5684
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
4348 VISIT(c, expr, item->context_expr);
5685 /* Will push bound __exit__ */
5686
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
4348 ADDOP(c, BEFORE_WITH);
5687
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
4348 ADDOP_JUMP(c, SETUP_WITH, final);
5688
5689 /* SETUP_WITH pushes a finally block. */
5690 4348 compiler_use_next_block(c, block);
5691
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
4348 if (!compiler_push_fblock(c, WITH, block, final, s)) {
5692 return 0;
5693 }
5694
5695
2/2
✓ Branch 0 taken 1960 times.
✓ Branch 1 taken 2388 times.
4348 if (item->optional_vars) {
5696
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1960 times.
1960 VISIT(c, expr, item->optional_vars);
5697 }
5698 else {
5699 /* Discard result from context.__enter__() */
5700
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2388 times.
2388 ADDOP(c, POP_TOP);
5701 }
5702
5703 4348 pos++;
5704
3/4
✓ Branch 0 taken 4348 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4330 times.
✓ Branch 3 taken 18 times.
4348 if (pos == asdl_seq_LEN(s->v.With.items))
5705 /* BLOCK code */
5706
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 9294 times.
✓ Branch 3 taken 13624 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 9294 times.
✓ Branch 6 taken 4330 times.
13624 VISIT_SEQ(c, stmt, s->v.With.body)
5707
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
18 else if (!compiler_with(c, s, pos))
5708 return 0;
5709
5710
5711 /* Mark all following code as artificial */
5712 4348 UNSET_LOC(c);
5713
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
4348 ADDOP(c, POP_BLOCK);
5714 4348 compiler_pop_fblock(c, WITH, block);
5715
5716 /* End of body; start the cleanup. */
5717
5718 /* For successful outcome:
5719 * call __exit__(None, None, None)
5720 */
5721 4348 SET_LOC(c, s);
5722
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
4348 if (!compiler_call_exit_with_nones(c))
5723 return 0;
5724
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
4348 ADDOP(c, POP_TOP);
5725
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
4348 ADDOP_JUMP(c, JUMP, exit);
5726
5727 /* For exceptional outcome: */
5728 4348 compiler_use_next_block(c, final);
5729
5730
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
4348 ADDOP_JUMP(c, SETUP_CLEANUP, cleanup);
5731
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
4348 ADDOP(c, PUSH_EXC_INFO);
5732
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4348 times.
4348 ADDOP(c, WITH_EXCEPT_START);
5733 4348 compiler_with_except_finish(c, cleanup);
5734
5735 4348 compiler_use_next_block(c, exit);
5736 4348 return 1;
5737 }
5738
5739 static int
5740 10270270 compiler_visit_expr1(struct compiler *c, expr_ty e)
5741 {
5742
26/28
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 12787 times.
✓ Branch 2 taken 1168328 times.
✓ Branch 3 taken 47921 times.
✓ Branch 4 taken 100164 times.
✓ Branch 5 taken 5964 times.
✓ Branch 6 taken 19396 times.
✓ Branch 7 taken 1609 times.
✓ Branch 8 taken 5547 times.
✓ Branch 9 taken 9279 times.
✓ Branch 10 taken 351 times.
✓ Branch 11 taken 828 times.
✓ Branch 12 taken 4712 times.
✓ Branch 13 taken 546 times.
✓ Branch 14 taken 1068 times.
✓ Branch 15 taken 169368 times.
✓ Branch 16 taken 1377634 times.
✓ Branch 17 taken 1549177 times.
✓ Branch 18 taken 10984 times.
✓ Branch 19 taken 33769 times.
✓ Branch 20 taken 517836 times.
✓ Branch 21 taken 154506 times.
✗ Branch 22 not taken.
✓ Branch 23 taken 11374 times.
✓ Branch 24 taken 4801369 times.
✓ Branch 25 taken 64045 times.
✓ Branch 26 taken 201684 times.
✗ Branch 27 not taken.
10270270 switch (e->kind) {
5743 24 case NamedExpr_kind:
5744
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 VISIT(c, expr, e->v.NamedExpr.value);
5745
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 ADDOP_I(c, COPY, 1);
5746
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 VISIT(c, expr, e->v.NamedExpr.target);
5747 24 break;
5748 12787 case BoolOp_kind:
5749 12787 return compiler_boolop(c, e);
5750 1168328 case BinOp_kind:
5751
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1168328 times.
1168328 VISIT(c, expr, e->v.BinOp.left);
5752
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1168328 times.
1168328 VISIT(c, expr, e->v.BinOp.right);
5753
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1168328 times.
1168328 ADDOP_BINARY(c, e->v.BinOp.op);
5754 1168328 break;
5755 47921 case UnaryOp_kind:
5756
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47921 times.
47921 VISIT(c, expr, e->v.UnaryOp.operand);
5757
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 47921 times.
47921 ADDOP(c, unaryop(e->v.UnaryOp.op));
5758 47921 break;
5759 100164 case Lambda_kind:
5760 100164 return compiler_lambda(c, e);
5761 5964 case IfExp_kind:
5762 5964 return compiler_ifexp(c, e);
5763 19396 case Dict_kind:
5764 19396 return compiler_dict(c, e);
5765 1609 case Set_kind:
5766 1609 return compiler_set(c, e);
5767 5547 case GeneratorExp_kind:
5768 5547 return compiler_genexp(c, e);
5769 9279 case ListComp_kind:
5770 9279 return compiler_listcomp(c, e);
5771 351 case SetComp_kind:
5772 351 return compiler_setcomp(c, e);
5773 828 case DictComp_kind:
5774 828 return compiler_dictcomp(c, e);
5775 4712 case Yield_kind:
5776
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4712 times.
4712 if (c->u->u_ste->ste_type != FunctionBlock)
5777 return compiler_error(c, "'yield' outside function");
5778
2/2
✓ Branch 0 taken 4527 times.
✓ Branch 1 taken 185 times.
4712 if (e->v.Yield.value) {
5779
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4527 times.
4527 VISIT(c, expr, e->v.Yield.value);
5780 }
5781 else {
5782
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
185 ADDOP_LOAD_CONST(c, Py_None);
5783 }
5784
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4712 times.
4712 ADDOP_YIELD(c);
5785 4712 break;
5786 546 case YieldFrom_kind:
5787
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 546 times.
546 if (c->u->u_ste->ste_type != FunctionBlock)
5788 return compiler_error(c, "'yield' outside function");
5789
5790
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 546 times.
546 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5791 return compiler_error(c, "'yield from' inside async function");
5792
5793
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 546 times.
546 VISIT(c, expr, e->v.YieldFrom.value);
5794
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 546 times.
546 ADDOP(c, GET_YIELD_FROM_ITER);
5795
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 546 times.
546 ADDOP_LOAD_CONST(c, Py_None);
5796
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 546 times.
546 ADD_YIELD_FROM(c, 0);
5797 546 break;
5798 1068 case Await_kind:
5799
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1068 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1068 if (!IS_TOP_LEVEL_AWAIT(c)){
5800
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1068 times.
1068 if (c->u->u_ste->ste_type != FunctionBlock){
5801 return compiler_error(c, "'await' outside function");
5802 }
5803
5804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1068 times.
1068 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
5805 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5806 return compiler_error(c, "'await' outside async function");
5807 }
5808 }
5809
5810
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1068 times.
1068 VISIT(c, expr, e->v.Await.value);
5811
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1068 times.
1068 ADDOP_I(c, GET_AWAITABLE, 0);
5812
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1068 times.
1068 ADDOP_LOAD_CONST(c, Py_None);
5813
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1068 times.
1068 ADD_YIELD_FROM(c, 1);
5814 1068 break;
5815 169368 case Compare_kind:
5816 169368 return compiler_compare(c, e);
5817 1377634 case Call_kind:
5818 1377634 return compiler_call(c, e);
5819 1549177 case Constant_kind:
5820
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1549177 times.
1549177 ADDOP_LOAD_CONST(c, e->v.Constant.value);
5821 1549177 break;
5822 10984 case JoinedStr_kind:
5823 10984 return compiler_joined_str(c, e);
5824 33769 case FormattedValue_kind:
5825 33769 return compiler_formatted_value(c, e);
5826 /* The following exprs can be assignment targets. */
5827 517836 case Attribute_kind:
5828
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 517836 times.
517836 VISIT(c, expr, e->v.Attribute.value);
5829
3/4
✓ Branch 0 taken 448229 times.
✓ Branch 1 taken 69453 times.
✓ Branch 2 taken 154 times.
✗ Branch 3 not taken.
517836 switch (e->v.Attribute.ctx) {
5830 448229 case Load:
5831 {
5832 448229 int old_lineno = c->u->u_loc.lineno;
5833 448229 c->u->u_loc.lineno = e->end_lineno;
5834
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448229 times.
448229 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5835 448229 c->u->u_loc.lineno = old_lineno;
5836 448229 break;
5837 }
5838 69453 case Store:
5839
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 69453 times.
69453 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) {
5840 return 0;
5841 }
5842 69453 int old_lineno = c->u->u_loc.lineno;
5843 69453 c->u->u_loc.lineno = e->end_lineno;
5844
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 69453 times.
69453 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5845 69453 c->u->u_loc.lineno = old_lineno;
5846 69453 break;
5847 154 case Del:
5848
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 154 times.
154 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5849 154 break;
5850 }
5851 517836 break;
5852 154506 case Subscript_kind:
5853 154506 return compiler_subscript(c, e);
5854 case Starred_kind:
5855 switch (e->v.Starred.ctx) {
5856 case Store:
5857 /* In all legitimate cases, the Starred node was already replaced
5858 * by compiler_list/compiler_tuple. XXX: is that okay? */
5859 return compiler_error(c,
5860 "starred assignment target must be in a list or tuple");
5861 default:
5862 return compiler_error(c,
5863 "can't use starred expression here");
5864 }
5865 break;
5866 11374 case Slice_kind:
5867 11374 return compiler_slice(c, e);
5868 4801369 case Name_kind:
5869 4801369 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5870 /* child nodes of List and Tuple will have expr_context set */
5871 64045 case List_kind:
5872 64045 return compiler_list(c, e);
5873 201684 case Tuple_kind:
5874 201684 return compiler_tuple(c, e);
5875 }
5876 3289612 return 1;
5877 }
5878
5879 static int
5880 10270270 compiler_visit_expr(struct compiler *c, expr_ty e)
5881 {
5882 10270270 struct location old_loc = c->u->u_loc;
5883 10270270 SET_LOC(c, e);
5884 10270270 int res = compiler_visit_expr1(c, e);
5885 10270270 c->u->u_loc = old_loc;
5886 10270270 return res;
5887 }
5888
5889 static int
5890 15223 compiler_augassign(struct compiler *c, stmt_ty s)
5891 {
5892 assert(s->kind == AugAssign_kind);
5893 15223 expr_ty e = s->v.AugAssign.target;
5894
5895 15223 struct location old_loc = c->u->u_loc;
5896 15223 SET_LOC(c, e);
5897
5898
3/4
✓ Branch 0 taken 1866 times.
✓ Branch 1 taken 1931 times.
✓ Branch 2 taken 11426 times.
✗ Branch 3 not taken.
15223 switch (e->kind) {
5899 1866 case Attribute_kind:
5900
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1866 times.
1866 VISIT(c, expr, e->v.Attribute.value);
5901
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1866 times.
1866 ADDOP_I(c, COPY, 1);
5902 1866 int old_lineno = c->u->u_loc.lineno;
5903 1866 c->u->u_loc.lineno = e->end_lineno;
5904
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1866 times.
1866 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5905 1866 c->u->u_loc.lineno = old_lineno;
5906 1866 break;
5907 1931 case Subscript_kind:
5908
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1931 times.
1931 VISIT(c, expr, e->v.Subscript.value);
5909
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1931 times.
1931 VISIT(c, expr, e->v.Subscript.slice);
5910
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1931 times.
1931 ADDOP_I(c, COPY, 2);
5911
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1931 times.
1931 ADDOP_I(c, COPY, 2);
5912
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1931 times.
1931 ADDOP(c, BINARY_SUBSCR);
5913 1931 break;
5914 11426 case Name_kind:
5915
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11426 times.
11426 if (!compiler_nameop(c, e->v.Name.id, Load))
5916 return 0;
5917 11426 break;
5918 default:
5919 PyErr_Format(PyExc_SystemError,
5920 "invalid node type (%d) for augmented assignment",
5921 e->kind);
5922 return 0;
5923 }
5924
5925 15223 c->u->u_loc = old_loc;
5926
5927
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15223 times.
15223 VISIT(c, expr, s->v.AugAssign.value);
5928
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15223 times.
15223 ADDOP_INPLACE(c, s->v.AugAssign.op);
5929
5930 15223 SET_LOC(c, e);
5931
5932
3/4
✓ Branch 0 taken 1866 times.
✓ Branch 1 taken 1931 times.
✓ Branch 2 taken 11426 times.
✗ Branch 3 not taken.
15223 switch (e->kind) {
5933 1866 case Attribute_kind:
5934 1866 c->u->u_loc.lineno = e->end_lineno;
5935
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1866 times.
1866 ADDOP_I(c, SWAP, 2);
5936
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1866 times.
1866 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5937 1866 break;
5938 1931 case Subscript_kind:
5939
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1931 times.
1931 ADDOP_I(c, SWAP, 3);
5940
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1931 times.
1931 ADDOP_I(c, SWAP, 2);
5941
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1931 times.
1931 ADDOP(c, STORE_SUBSCR);
5942 1931 break;
5943 11426 case Name_kind:
5944 11426 return compiler_nameop(c, e->v.Name.id, Store);
5945 default:
5946 Py_UNREACHABLE();
5947 }
5948 3797 return 1;
5949 }
5950
5951 static int
5952 8 check_ann_expr(struct compiler *c, expr_ty e)
5953 {
5954
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 VISIT(c, expr, e);
5955
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
8 ADDOP(c, POP_TOP);
5956 8 return 1;
5957 }
5958
5959 static int
5960 452 check_annotation(struct compiler *c, stmt_ty s)
5961 {
5962 /* Annotations of complex targets does not produce anything
5963 under annotations future */
5964
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 446 times.
452 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5965 6 return 1;
5966 }
5967
5968 /* Annotations are only evaluated in a module or class. */
5969
1/2
✓ Branch 0 taken 446 times.
✗ Branch 1 not taken.
446 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 446 times.
446 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5971 return check_ann_expr(c, s->v.AnnAssign.annotation);
5972 }
5973 446 return 1;
5974 }
5975
5976 static int
5977 check_ann_subscr(struct compiler *c, expr_ty e)
5978 {
5979 /* We check that everything in a subscript is defined at runtime. */
5980 switch (e->kind) {
5981 case Slice_kind:
5982 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
5983 return 0;
5984 }
5985 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5986 return 0;
5987 }
5988 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5989 return 0;
5990 }
5991 return 1;
5992 case Tuple_kind: {
5993 /* extended slice */
5994 asdl_expr_seq *elts = e->v.Tuple.elts;
5995 Py_ssize_t i, n = asdl_seq_LEN(elts);
5996 for (i = 0; i < n; i++) {
5997 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
5998 return 0;
5999 }
6000 }
6001 return 1;
6002 }
6003 default:
6004 return check_ann_expr(c, e);
6005 }
6006 }
6007
6008 static int
6009 2553 compiler_annassign(struct compiler *c, stmt_ty s)
6010 {
6011 2553 expr_ty targ = s->v.AnnAssign.target;
6012 PyObject* mangled;
6013
6014 assert(s->kind == AnnAssign_kind);
6015
6016 /* We perform the actual assignment first. */
6017
2/2
✓ Branch 0 taken 1894 times.
✓ Branch 1 taken 659 times.
2553 if (s->v.AnnAssign.value) {
6018
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1894 times.
1894 VISIT(c, expr, s->v.AnnAssign.value);
6019
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1894 times.
1894 VISIT(c, expr, targ);
6020 }
6021
2/4
✓ Branch 0 taken 2101 times.
✓ Branch 1 taken 452 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2553 switch (targ->kind) {
6022 2101 case Name_kind:
6023
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2101 times.
2101 if (forbidden_name(c, targ->v.Name.id, Store))
6024 return 0;
6025 /* If we have a simple name in a module or class, store annotation. */
6026
1/2
✓ Branch 0 taken 2101 times.
✗ Branch 1 not taken.
2101 if (s->v.AnnAssign.simple &&
6027
2/2
✓ Branch 0 taken 1779 times.
✓ Branch 1 taken 322 times.
2101 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
6028
2/2
✓ Branch 0 taken 1026 times.
✓ Branch 1 taken 753 times.
1779 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
6029
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1344 times.
1348 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
6030
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 VISIT(c, annexpr, s->v.AnnAssign.annotation)
6031 }
6032 else {
6033
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1344 times.
1344 VISIT(c, expr, s->v.AnnAssign.annotation);
6034 }
6035
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1348 times.
1348 ADDOP_NAME(c, LOAD_NAME, &_Py_ID(__annotations__), names);
6036 1348 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
6037
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1348 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1348 times.
1348 ADDOP_LOAD_CONST_NEW(c, mangled);
6038
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1348 times.
1348 ADDOP(c, STORE_SUBSCR);
6039 }
6040 2101 break;
6041 452 case Attribute_kind:
6042
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 452 times.
452 if (forbidden_name(c, targ->v.Attribute.attr, Store))
6043 return 0;
6044
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 444 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
460 if (!s->v.AnnAssign.value &&
6045 8 !check_ann_expr(c, targ->v.Attribute.value)) {
6046 return 0;
6047 }
6048 452 break;
6049 case Subscript_kind:
6050 if (!s->v.AnnAssign.value &&
6051 (!check_ann_expr(c, targ->v.Subscript.value) ||
6052 !check_ann_subscr(c, targ->v.Subscript.slice))) {
6053 return 0;
6054 }
6055 break;
6056 default:
6057 PyErr_Format(PyExc_SystemError,
6058 "invalid node type (%d) for annotated assignment",
6059 targ->kind);
6060 return 0;
6061 }
6062 /* Annotation is evaluated last. */
6063
3/4
✓ Branch 0 taken 452 times.
✓ Branch 1 taken 2101 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 452 times.
2553 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
6064 return 0;
6065 }
6066 2553 return 1;
6067 }
6068
6069 /* Raises a SyntaxError and returns 0.
6070 If something goes wrong, a different exception may be raised.
6071 */
6072
6073 static int
6074 compiler_error(struct compiler *c, const char *format, ...)
6075 {
6076 va_list vargs;
6077 va_start(vargs, format);
6078 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
6079 va_end(vargs);
6080 if (msg == NULL) {
6081 return 0;
6082 }
6083 PyObject *loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_loc.lineno);
6084 if (loc == NULL) {
6085 Py_INCREF(Py_None);
6086 loc = Py_None;
6087 }
6088 struct location u_loc = c->u->u_loc;
6089 PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
6090 u_loc.lineno, u_loc.col_offset + 1, loc,
6091 u_loc.end_lineno, u_loc.end_col_offset + 1);
6092 Py_DECREF(msg);
6093 if (args == NULL) {
6094 goto exit;
6095 }
6096 PyErr_SetObject(PyExc_SyntaxError, args);
6097 exit:
6098 Py_DECREF(loc);
6099 Py_XDECREF(args);
6100 return 0;
6101 }
6102
6103 /* Emits a SyntaxWarning and returns 1 on success.
6104 If a SyntaxWarning raised as error, replaces it with a SyntaxError
6105 and returns 0.
6106 */
6107 static int
6108 compiler_warn(struct compiler *c, const char *format, ...)
6109 {
6110 va_list vargs;
6111 va_start(vargs, format);
6112 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
6113 va_end(vargs);
6114 if (msg == NULL) {
6115 return 0;
6116 }
6117 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
6118 c->u->u_loc.lineno, NULL, NULL) < 0)
6119 {
6120 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
6121 /* Replace the SyntaxWarning exception with a SyntaxError
6122 to get a more accurate error report */
6123 PyErr_Clear();
6124 assert(PyUnicode_AsUTF8(msg) != NULL);
6125 compiler_error(c, PyUnicode_AsUTF8(msg));
6126 }
6127 Py_DECREF(msg);
6128 return 0;
6129 }
6130 Py_DECREF(msg);
6131 return 1;
6132 }
6133
6134 static int
6135 154506 compiler_subscript(struct compiler *c, expr_ty e)
6136 {
6137 154506 expr_context_ty ctx = e->v.Subscript.ctx;
6138 154506 int op = 0;
6139
6140
2/2
✓ Branch 0 taken 132329 times.
✓ Branch 1 taken 22177 times.
154506 if (ctx == Load) {
6141
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 132329 times.
132329 if (!check_subscripter(c, e->v.Subscript.value)) {
6142 return 0;
6143 }
6144
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 132329 times.
132329 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
6145 return 0;
6146 }
6147 }
6148
6149
3/4
✓ Branch 0 taken 132329 times.
✓ Branch 1 taken 21004 times.
✓ Branch 2 taken 1173 times.
✗ Branch 3 not taken.
154506 switch (ctx) {
6150 132329 case Load: op = BINARY_SUBSCR; break;
6151 21004 case Store: op = STORE_SUBSCR; break;
6152 1173 case Del: op = DELETE_SUBSCR; break;
6153 }
6154 assert(op);
6155
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 154506 times.
154506 VISIT(c, expr, e->v.Subscript.value);
6156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 154506 times.
154506 VISIT(c, expr, e->v.Subscript.slice);
6157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 154506 times.
154506 ADDOP(c, op);
6158 154506 return 1;
6159 }
6160
6161 static int
6162 11374 compiler_slice(struct compiler *c, expr_ty s)
6163 {
6164 11374 int n = 2;
6165 assert(s->kind == Slice_kind);
6166
6167 /* only handles the cases where BUILD_SLICE is emitted */
6168
2/2
✓ Branch 0 taken 6016 times.
✓ Branch 1 taken 5358 times.
11374 if (s->v.Slice.lower) {
6169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6016 times.
6016 VISIT(c, expr, s->v.Slice.lower);
6170 }
6171 else {
6172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5358 times.
5358 ADDOP_LOAD_CONST(c, Py_None);
6173 }
6174
6175
2/2
✓ Branch 0 taken 5086 times.
✓ Branch 1 taken 6288 times.
11374 if (s->v.Slice.upper) {
6176
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5086 times.
5086 VISIT(c, expr, s->v.Slice.upper);
6177 }
6178 else {
6179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6288 times.
6288 ADDOP_LOAD_CONST(c, Py_None);
6180 }
6181
6182
2/2
✓ Branch 0 taken 455 times.
✓ Branch 1 taken 10919 times.
11374 if (s->v.Slice.step) {
6183 455 n++;
6184
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 455 times.
455 VISIT(c, expr, s->v.Slice.step);
6185 }
6186
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11374 times.
11374 ADDOP_I(c, BUILD_SLICE, n);
6187 11374 return 1;
6188 }
6189
6190
6191 // PEP 634: Structural Pattern Matching
6192
6193 // To keep things simple, all compiler_pattern_* and pattern_helper_* routines
6194 // follow the convention of consuming TOS (the subject for the given pattern)
6195 // and calling jump_to_fail_pop on failure (no match).
6196
6197 // When calling into these routines, it's important that pc->on_top be kept
6198 // updated to reflect the current number of items that we are using on the top
6199 // of the stack: they will be popped on failure, and any name captures will be
6200 // stored *underneath* them on success. This lets us defer all names stores
6201 // until the *entire* pattern matches.
6202
6203 #define WILDCARD_CHECK(N) \
6204 ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name)
6205
6206 #define WILDCARD_STAR_CHECK(N) \
6207 ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
6208
6209 // Limit permitted subexpressions, even if the parser & AST validator let them through
6210 #define MATCH_VALUE_EXPR(N) \
6211 ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
6212
6213 // Allocate or resize pc->fail_pop to allow for n items to be popped on failure.
6214 static int
6215 6 ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
6216 {
6217 6 Py_ssize_t size = n + 1;
6218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (size <= pc->fail_pop_size) {
6219 return 1;
6220 }
6221 6 Py_ssize_t needed = sizeof(basicblock*) * size;
6222 6 basicblock **resized = PyObject_Realloc(pc->fail_pop, needed);
6223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (resized == NULL) {
6224 PyErr_NoMemory();
6225 return 0;
6226 }
6227 6 pc->fail_pop = resized;
6228
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6 times.
16 while (pc->fail_pop_size < size) {
6229 basicblock *new_block;
6230
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
10 RETURN_IF_FALSE(new_block = compiler_new_block(c));
6231 10 pc->fail_pop[pc->fail_pop_size++] = new_block;
6232 }
6233 6 return 1;
6234 }
6235
6236 // Use op to jump to the correct fail_pop block.
6237 static int
6238 5 jump_to_fail_pop(struct compiler *c, pattern_context *pc, int op)
6239 {
6240 // Pop any items on the top of the stack, plus any objects we were going to
6241 // capture on success:
6242 5 Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores);
6243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 RETURN_IF_FALSE(ensure_fail_pop(c, pc, pops));
6244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 ADDOP_JUMP(c, op, pc->fail_pop[pops]);
6245 5 return 1;
6246 }
6247
6248 // Build all of the fail_pop blocks and reset fail_pop.
6249 static int
6250 6 emit_and_reset_fail_pop(struct compiler *c, pattern_context *pc)
6251 {
6252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!pc->fail_pop_size) {
6253 assert(pc->fail_pop == NULL);
6254 return 1;
6255 }
6256
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 while (--pc->fail_pop_size) {
6257 4 compiler_use_next_block(c, pc->fail_pop[pc->fail_pop_size]);
6258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!compiler_addop(c, POP_TOP, true)) {
6259 pc->fail_pop_size = 0;
6260 PyObject_Free(pc->fail_pop);
6261 pc->fail_pop = NULL;
6262 return 0;
6263 }
6264 }
6265 6 compiler_use_next_block(c, pc->fail_pop[0]);
6266 6 PyObject_Free(pc->fail_pop);
6267 6 pc->fail_pop = NULL;
6268 6 return 1;
6269 }
6270
6271 static int
6272 compiler_error_duplicate_store(struct compiler *c, identifier n)
6273 {
6274 return compiler_error(c, "multiple assignments to name %R in pattern", n);
6275 }
6276
6277 // Duplicate the effect of 3.10's ROT_* instructions using SWAPs.
6278 static int
6279 3 pattern_helper_rotate(struct compiler *c, Py_ssize_t count)
6280 {
6281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 while (1 < count) {
6282 ADDOP_I(c, SWAP, count--);
6283 }
6284 3 return 1;
6285 }
6286
6287 static int
6288 3 pattern_helper_store_name(struct compiler *c, identifier n, pattern_context *pc)
6289 {
6290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (n == NULL) {
6291 ADDOP(c, POP_TOP);
6292 return 1;
6293 }
6294
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (forbidden_name(c, n, Store)) {
6295 return 0;
6296 }
6297 // Can't assign to the same name twice:
6298 3 int duplicate = PySequence_Contains(pc->stores, n);
6299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (duplicate < 0) {
6300 return 0;
6301 }
6302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (duplicate) {
6303 return compiler_error_duplicate_store(c, n);
6304 }
6305 // Rotate this object underneath any items we need to preserve:
6306 3 Py_ssize_t rotations = pc->on_top + PyList_GET_SIZE(pc->stores) + 1;
6307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 RETURN_IF_FALSE(pattern_helper_rotate(c, rotations));
6308 3 return !PyList_Append(pc->stores, n);
6309 }
6310
6311
6312 static int
6313 pattern_unpack_helper(struct compiler *c, asdl_pattern_seq *elts)
6314 {
6315 Py_ssize_t n = asdl_seq_LEN(elts);
6316 int seen_star = 0;
6317 for (Py_ssize_t i = 0; i < n; i++) {
6318 pattern_ty elt = asdl_seq_GET(elts, i);
6319 if (elt->kind == MatchStar_kind && !seen_star) {
6320 if ((i >= (1 << 8)) ||
6321 (n-i-1 >= (INT_MAX >> 8)))
6322 return compiler_error(c,
6323 "too many expressions in "
6324 "star-unpacking sequence pattern");
6325 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
6326 seen_star = 1;
6327 }
6328 else if (elt->kind == MatchStar_kind) {
6329 return compiler_error(c,
6330 "multiple starred expressions in sequence pattern");
6331 }
6332 }
6333 if (!seen_star) {
6334 ADDOP_I(c, UNPACK_SEQUENCE, n);
6335 }
6336 return 1;
6337 }
6338
6339 static int
6340 pattern_helper_sequence_unpack(struct compiler *c, asdl_pattern_seq *patterns,
6341 Py_ssize_t star, pattern_context *pc)
6342 {
6343 RETURN_IF_FALSE(pattern_unpack_helper(c, patterns));
6344 Py_ssize_t size = asdl_seq_LEN(patterns);
6345 // We've now got a bunch of new subjects on the stack. They need to remain
6346 // there after each subpattern match:
6347 pc->on_top += size;
6348 for (Py_ssize_t i = 0; i < size; i++) {
6349 // One less item to keep track of each time we loop through:
6350 pc->on_top--;
6351 pattern_ty pattern = asdl_seq_GET(patterns, i);
6352 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
6353 }
6354 return 1;
6355 }
6356
6357 // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
6358 // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
6359 // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
6360 static int
6361 pattern_helper_sequence_subscr(struct compiler *c, asdl_pattern_seq *patterns,
6362 Py_ssize_t star, pattern_context *pc)
6363 {
6364 // We need to keep the subject around for extracting elements:
6365 pc->on_top++;
6366 Py_ssize_t size = asdl_seq_LEN(patterns);
6367 for (Py_ssize_t i = 0; i < size; i++) {
6368 pattern_ty pattern = asdl_seq_GET(patterns, i);
6369 if (WILDCARD_CHECK(pattern)) {
6370 continue;
6371 }
6372 if (i == star) {
6373 assert(WILDCARD_STAR_CHECK(pattern));
6374 continue;
6375 }
6376 ADDOP_I(c, COPY, 1);
6377 if (i < star) {
6378 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(i));
6379 }
6380 else {
6381 // The subject may not support negative indexing! Compute a
6382 // nonnegative index:
6383 ADDOP(c, GET_LEN);
6384 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - i));
6385 ADDOP_BINARY(c, Sub);
6386 }
6387 ADDOP(c, BINARY_SUBSCR);
6388 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
6389 }
6390 // Pop the subject, we're done with it:
6391 pc->on_top--;
6392 ADDOP(c, POP_TOP);
6393 return 1;
6394 }
6395
6396 // Like compiler_pattern, but turn off checks for irrefutability.
6397 static int
6398 2 compiler_pattern_subpattern(struct compiler *c, pattern_ty p, pattern_context *pc)
6399 {
6400 2 int allow_irrefutable = pc->allow_irrefutable;
6401 2 pc->allow_irrefutable = 1;
6402
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 RETURN_IF_FALSE(compiler_pattern(c, p, pc));
6403 2 pc->allow_irrefutable = allow_irrefutable;
6404 2 return 1;
6405 }
6406
6407 static int
6408 3 compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
6409 {
6410 assert(p->kind == MatchAs_kind);
6411
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (p->v.MatchAs.pattern == NULL) {
6412 // An irrefutable match:
6413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!pc->allow_irrefutable) {
6414 if (p->v.MatchAs.name) {
6415 const char *e = "name capture %R makes remaining patterns unreachable";
6416 return compiler_error(c, e, p->v.MatchAs.name);
6417 }
6418 const char *e = "wildcard makes remaining patterns unreachable";
6419 return compiler_error(c, e);
6420 }
6421 3 return pattern_helper_store_name(c, p->v.MatchAs.name, pc);
6422 }
6423 // Need to make a copy for (possibly) storing later:
6424 pc->on_top++;
6425 ADDOP_I(c, COPY, 1);
6426 RETURN_IF_FALSE(compiler_pattern(c, p->v.MatchAs.pattern, pc));
6427 // Success! Store it:
6428 pc->on_top--;
6429 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchAs.name, pc));
6430 return 1;
6431 }
6432
6433 static int
6434 compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
6435 {
6436 assert(p->kind == MatchStar_kind);
6437 RETURN_IF_FALSE(pattern_helper_store_name(c, p->v.MatchStar.name, pc));
6438 return 1;
6439 }
6440
6441 static int
6442 validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns)
6443 {
6444 // Any errors will point to the pattern rather than the arg name as the
6445 // parser is only supplying identifiers rather than Name or keyword nodes
6446 Py_ssize_t nattrs = asdl_seq_LEN(attrs);
6447 for (Py_ssize_t i = 0; i < nattrs; i++) {
6448 identifier attr = ((identifier)asdl_seq_GET(attrs, i));
6449 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
6450 if (forbidden_name(c, attr, Store)) {
6451 return -1;
6452 }
6453 for (Py_ssize_t j = i + 1; j < nattrs; j++) {
6454 identifier other = ((identifier)asdl_seq_GET(attrs, j));
6455 if (!PyUnicode_Compare(attr, other)) {
6456 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j)));
6457 compiler_error(c, "attribute name repeated in class pattern: %U", attr);
6458 return -1;
6459 }
6460 }
6461 }
6462 return 0;
6463 }
6464
6465 static int
6466 4 compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
6467 {
6468 assert(p->kind == MatchClass_kind);
6469 4 asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
6470 4 asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs;
6471 4 asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns;
6472
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 Py_ssize_t nargs = asdl_seq_LEN(patterns);
6473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs);
6474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns);
6475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (nattrs != nkwd_patterns) {
6476 // AST validator shouldn't let this happen, but if it does,
6477 // just fail, don't crash out of the interpreter
6478 const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
6479 return compiler_error(c, e, nattrs, nkwd_patterns);
6480 }
6481
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
6482 const char *e = "too many sub-patterns in class pattern %R";
6483 return compiler_error(c, e, p->v.MatchClass.cls);
6484 }
6485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (nattrs) {
6486 RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
6487 SET_LOC(c, p);
6488 }
6489
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 VISIT(c, expr, p->v.MatchClass.cls);
6490 PyObject *attr_names;
6491
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 RETURN_IF_FALSE(attr_names = PyTuple_New(nattrs));
6492 Py_ssize_t i;
6493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 for (i = 0; i < nattrs; i++) {
6494 PyObject *name = asdl_seq_GET(kwd_attrs, i);
6495 Py_INCREF(name);
6496 PyTuple_SET_ITEM(attr_names, i, name);
6497 }
6498
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
4 ADDOP_LOAD_CONST_NEW(c, attr_names);
6499
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 ADDOP_I(c, MATCH_CLASS, nargs);
6500
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 ADDOP_I(c, COPY, 1);
6501
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 ADDOP_LOAD_CONST(c, Py_None);
6502
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 ADDOP_I(c, IS_OP, 1);
6503 // TOS is now a tuple of (nargs + nattrs) attributes (or None):
6504 4 pc->on_top++;
6505
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6506
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 ADDOP_I(c, UNPACK_SEQUENCE, nargs + nattrs);
6507 4 pc->on_top += nargs + nattrs - 1;
6508
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 for (i = 0; i < nargs + nattrs; i++) {
6509 2 pc->on_top--;
6510 pattern_ty pattern;
6511
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (i < nargs) {
6512 // Positional:
6513 2 pattern = asdl_seq_GET(patterns, i);
6514 }
6515 else {
6516 // Keyword:
6517 pattern = asdl_seq_GET(kwd_patterns, i - nargs);
6518 }
6519
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (WILDCARD_CHECK(pattern)) {
6520 ADDOP(c, POP_TOP);
6521 continue;
6522 }
6523
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
6524 }
6525 // Success! Pop the tuple of attributes:
6526 4 return 1;
6527 }
6528
6529 static int
6530 compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
6531 {
6532 assert(p->kind == MatchMapping_kind);
6533 asdl_expr_seq *keys = p->v.MatchMapping.keys;
6534 asdl_pattern_seq *patterns = p->v.MatchMapping.patterns;
6535 Py_ssize_t size = asdl_seq_LEN(keys);
6536 Py_ssize_t npatterns = asdl_seq_LEN(patterns);
6537 if (size != npatterns) {
6538 // AST validator shouldn't let this happen, but if it does,
6539 // just fail, don't crash out of the interpreter
6540 const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
6541 return compiler_error(c, e, size, npatterns);
6542 }
6543 // We have a double-star target if "rest" is set
6544 PyObject *star_target = p->v.MatchMapping.rest;
6545 // We need to keep the subject on top during the mapping and length checks:
6546 pc->on_top++;
6547 ADDOP(c, MATCH_MAPPING);
6548 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6549 if (!size && !star_target) {
6550 // If the pattern is just "{}", we're done! Pop the subject:
6551 pc->on_top--;
6552 ADDOP(c, POP_TOP);
6553 return 1;
6554 }
6555 if (size) {
6556 // If the pattern has any keys in it, perform a length check:
6557 ADDOP(c, GET_LEN);
6558 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6559 ADDOP_COMPARE(c, GtE);
6560 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6561 }
6562 if (INT_MAX < size - 1) {
6563 return compiler_error(c, "too many sub-patterns in mapping pattern");
6564 }
6565 // Collect all of the keys into a tuple for MATCH_KEYS and
6566 // **rest. They can either be dotted names or literals:
6567
6568 // Maintaining a set of Constant_kind kind keys allows us to raise a
6569 // SyntaxError in the case of duplicates.
6570 PyObject *seen = PySet_New(NULL);
6571 if (seen == NULL) {
6572 return 0;
6573 }
6574
6575 // NOTE: goto error on failure in the loop below to avoid leaking `seen`
6576 for (Py_ssize_t i = 0; i < size; i++) {
6577 expr_ty key = asdl_seq_GET(keys, i);
6578 if (key == NULL) {
6579 const char *e = "can't use NULL keys in MatchMapping "
6580 "(set 'rest' parameter instead)";
6581 SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
6582 compiler_error(c, e);
6583 goto error;
6584 }
6585
6586 if (key->kind == Constant_kind) {
6587 int in_seen = PySet_Contains(seen, key->v.Constant.value);
6588 if (in_seen < 0) {
6589 goto error;
6590 }
6591 if (in_seen) {
6592 const char *e = "mapping pattern checks duplicate key (%R)";
6593 compiler_error(c, e, key->v.Constant.value);
6594 goto error;
6595 }
6596 if (PySet_Add(seen, key->v.Constant.value)) {
6597 goto error;
6598 }
6599 }
6600
6601 else if (key->kind != Attribute_kind) {
6602 const char *e = "mapping pattern keys may only match literals and attribute lookups";
6603 compiler_error(c, e);
6604 goto error;
6605 }
6606 if (!compiler_visit_expr(c, key)) {
6607 goto error;
6608 }
6609 }
6610
6611 // all keys have been checked; there are no duplicates
6612 Py_DECREF(seen);
6613
6614 ADDOP_I(c, BUILD_TUPLE, size);
6615 ADDOP(c, MATCH_KEYS);
6616 // There's now a tuple of keys and a tuple of values on top of the subject:
6617 pc->on_top += 2;
6618 ADDOP_I(c, COPY, 1);
6619 ADDOP_LOAD_CONST(c, Py_None);
6620 ADDOP_I(c, IS_OP, 1);
6621 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6622 // So far so good. Use that tuple of values on the stack to match
6623 // sub-patterns against:
6624 ADDOP_I(c, UNPACK_SEQUENCE, size);
6625 pc->on_top += size - 1;
6626 for (Py_ssize_t i = 0; i < size; i++) {
6627 pc->on_top--;
6628 pattern_ty pattern = asdl_seq_GET(patterns, i);
6629 RETURN_IF_FALSE(compiler_pattern_subpattern(c, pattern, pc));
6630 }
6631 // If we get this far, it's a match! Whatever happens next should consume
6632 // the tuple of keys and the subject:
6633 pc->on_top -= 2;
6634 if (star_target) {
6635 // If we have a starred name, bind a dict of remaining items to it (this may
6636 // seem a bit inefficient, but keys is rarely big enough to actually impact
6637 // runtime):
6638 // rest = dict(TOS1)
6639 // for key in TOS:
6640 // del rest[key]
6641 ADDOP_I(c, BUILD_MAP, 0); // [subject, keys, empty]
6642 ADDOP_I(c, SWAP, 3); // [empty, keys, subject]
6643 ADDOP_I(c, DICT_UPDATE, 2); // [copy, keys]
6644 ADDOP_I(c, UNPACK_SEQUENCE, size); // [copy, keys...]
6645 while (size) {
6646 ADDOP_I(c, COPY, 1 + size--); // [copy, keys..., copy]
6647 ADDOP_I(c, SWAP, 2); // [copy, keys..., copy, key]
6648 ADDOP(c, DELETE_SUBSCR); // [copy, keys...]
6649 }
6650 RETURN_IF_FALSE(pattern_helper_store_name(c, star_target, pc));
6651 }
6652 else {
6653 ADDOP(c, POP_TOP); // Tuple of keys.
6654 ADDOP(c, POP_TOP); // Subject.
6655 }
6656 return 1;
6657
6658 error:
6659 Py_DECREF(seen);
6660 return 0;
6661 }
6662
6663 static int
6664 compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
6665 {
6666 assert(p->kind == MatchOr_kind);
6667 basicblock *end;
6668 RETURN_IF_FALSE(end = compiler_new_block(c));
6669 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
6670 assert(size > 1);
6671 // We're going to be messing with pc. Keep the original info handy:
6672 pattern_context old_pc = *pc;
6673 Py_INCREF(pc->stores);
6674 // control is the list of names bound by the first alternative. It is used
6675 // for checking different name bindings in alternatives, and for correcting
6676 // the order in which extracted elements are placed on the stack.
6677 PyObject *control = NULL;
6678 // NOTE: We can't use returning macros anymore! goto error on error.
6679 for (Py_ssize_t i = 0; i < size; i++) {
6680 pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
6681 SET_LOC(c, alt);
6682 PyObject *pc_stores = PyList_New(0);
6683 if (pc_stores == NULL) {
6684 goto error;
6685 }
6686 Py_SETREF(pc->stores, pc_stores);
6687 // An irrefutable sub-pattern must be last, if it is allowed at all:
6688 pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable;
6689 pc->fail_pop = NULL;
6690 pc->fail_pop_size = 0;
6691 pc->on_top = 0;
6692 if (!compiler_addop_i(c, COPY, 1, true) || !compiler_pattern(c, alt, pc)) {
6693 goto error;
6694 }
6695 // Success!
6696 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
6697 if (!i) {
6698 // This is the first alternative, so save its stores as a "control"
6699 // for the others (they can't bind a different set of names, and
6700 // might need to be reordered):
6701 assert(control == NULL);
6702 control = pc->stores;
6703 Py_INCREF(control);
6704 }
6705 else if (nstores != PyList_GET_SIZE(control)) {
6706 goto diff;
6707 }
6708 else if (nstores) {
6709 // There were captures. Check to see if we differ from control:
6710 Py_ssize_t icontrol = nstores;
6711 while (icontrol--) {
6712 PyObject *name = PyList_GET_ITEM(control, icontrol);
6713 Py_ssize_t istores = PySequence_Index(pc->stores, name);
6714 if (istores < 0) {
6715 PyErr_Clear();
6716 goto diff;
6717 }
6718 if (icontrol != istores) {
6719 // Reorder the names on the stack to match the order of the
6720 // names in control. There's probably a better way of doing
6721 // this; the current solution is potentially very
6722 // inefficient when each alternative subpattern binds lots
6723 // of names in different orders. It's fine for reasonable
6724 // cases, though, and the peephole optimizer will ensure
6725 // that the final code is as efficient as possible.
6726 assert(istores < icontrol);
6727 Py_ssize_t rotations = istores + 1;
6728 // Perform the same rotation on pc->stores:
6729 PyObject *rotated = PyList_GetSlice(pc->stores, 0,
6730 rotations);
6731 if (rotated == NULL ||
6732 PyList_SetSlice(pc->stores, 0, rotations, NULL) ||
6733 PyList_SetSlice(pc->stores, icontrol - istores,
6734 icontrol - istores, rotated))
6735 {
6736 Py_XDECREF(rotated);
6737 goto error;
6738 }
6739 Py_DECREF(rotated);
6740 // That just did:
6741 // rotated = pc_stores[:rotations]
6742 // del pc_stores[:rotations]
6743 // pc_stores[icontrol-istores:icontrol-istores] = rotated
6744 // Do the same thing to the stack, using several
6745 // rotations:
6746 while (rotations--) {
6747 if (!pattern_helper_rotate(c, icontrol + 1)){
6748 goto error;
6749 }
6750 }
6751 }
6752 }
6753 }
6754 assert(control);
6755 if (!compiler_addop_j(c, JUMP, end, true) ||
6756 !emit_and_reset_fail_pop(c, pc))
6757 {
6758 goto error;
6759 }
6760 }
6761 Py_DECREF(pc->stores);
6762 *pc = old_pc;
6763 Py_INCREF(pc->stores);
6764 // Need to NULL this for the PyObject_Free call in the error block.
6765 old_pc.fail_pop = NULL;
6766 // No match. Pop the remaining copy of the subject and fail:
6767 if (!compiler_addop(c, POP_TOP, true) || !jump_to_fail_pop(c, pc, JUMP)) {
6768 goto error;
6769 }
6770 compiler_use_next_block(c, end);
6771 Py_ssize_t nstores = PyList_GET_SIZE(control);
6772 // There's a bunch of stuff on the stack between where the new stores
6773 // are and where they need to be:
6774 // - The other stores.
6775 // - A copy of the subject.
6776 // - Anything else that may be on top of the stack.
6777 // - Any previous stores we've already stashed away on the stack.
6778 Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores);
6779 for (Py_ssize_t i = 0; i < nstores; i++) {
6780 // Rotate this capture to its proper place on the stack:
6781 if (!pattern_helper_rotate(c, nrots)) {
6782 goto error;
6783 }
6784 // Update the list of previous stores with this new name, checking for
6785 // duplicates:
6786 PyObject *name = PyList_GET_ITEM(control, i);
6787 int dupe = PySequence_Contains(pc->stores, name);
6788 if (dupe < 0) {
6789 goto error;
6790 }
6791 if (dupe) {
6792 compiler_error_duplicate_store(c, name);
6793 goto error;
6794 }
6795 if (PyList_Append(pc->stores, name)) {
6796 goto error;
6797 }
6798 }
6799 Py_DECREF(old_pc.stores);
6800 Py_DECREF(control);
6801 // NOTE: Returning macros are safe again.
6802 // Pop the copy of the subject:
6803 ADDOP(c, POP_TOP);
6804 return 1;
6805 diff:
6806 compiler_error(c, "alternative patterns bind different names");
6807 error:
6808 PyObject_Free(old_pc.fail_pop);
6809 Py_DECREF(old_pc.stores);
6810 Py_XDECREF(control);
6811 return 0;
6812 }
6813
6814
6815 static int
6816 compiler_pattern_sequence(struct compiler *c, pattern_ty p, pattern_context *pc)
6817 {
6818 assert(p->kind == MatchSequence_kind);
6819 asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
6820 Py_ssize_t size = asdl_seq_LEN(patterns);
6821 Py_ssize_t star = -1;
6822 int only_wildcard = 1;
6823 int star_wildcard = 0;
6824 // Find a starred name, if it exists. There may be at most one:
6825 for (Py_ssize_t i = 0; i < size; i++) {
6826 pattern_ty pattern = asdl_seq_GET(patterns, i);
6827 if (pattern->kind == MatchStar_kind) {
6828 if (star >= 0) {
6829 const char *e = "multiple starred names in sequence pattern";
6830 return compiler_error(c, e);
6831 }
6832 star_wildcard = WILDCARD_STAR_CHECK(pattern);
6833 only_wildcard &= star_wildcard;
6834 star = i;
6835 continue;
6836 }
6837 only_wildcard &= WILDCARD_CHECK(pattern);
6838 }
6839 // We need to keep the subject on top during the sequence and length checks:
6840 pc->on_top++;
6841 ADDOP(c, MATCH_SEQUENCE);
6842 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6843 if (star < 0) {
6844 // No star: len(subject) == size
6845 ADDOP(c, GET_LEN);
6846 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size));
6847 ADDOP_COMPARE(c, Eq);
6848 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6849 }
6850 else if (size > 1) {
6851 // Star: len(subject) >= size - 1
6852 ADDOP(c, GET_LEN);
6853 ADDOP_LOAD_CONST_NEW(c, PyLong_FromSsize_t(size - 1));
6854 ADDOP_COMPARE(c, GtE);
6855 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6856 }
6857 // Whatever comes next should consume the subject:
6858 pc->on_top--;
6859 if (only_wildcard) {
6860 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
6861 ADDOP(c, POP_TOP);
6862 }
6863 else if (star_wildcard) {
6864 RETURN_IF_FALSE(pattern_helper_sequence_subscr(c, patterns, star, pc));
6865 }
6866 else {
6867 RETURN_IF_FALSE(pattern_helper_sequence_unpack(c, patterns, star, pc));
6868 }
6869 return 1;
6870 }
6871
6872 static int
6873 compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
6874 {
6875 assert(p->kind == MatchValue_kind);
6876 expr_ty value = p->v.MatchValue.value;
6877 if (!MATCH_VALUE_EXPR(value)) {
6878 const char *e = "patterns may only match literals and attribute lookups";
6879 return compiler_error(c, e);
6880 }
6881 VISIT(c, expr, value);
6882 ADDOP_COMPARE(c, Eq);
6883 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6884 return 1;
6885 }
6886
6887 static int
6888 1 compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc)
6889 {
6890 assert(p->kind == MatchSingleton_kind);
6891
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ADDOP_LOAD_CONST(c, p->v.MatchSingleton.value);
6892
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ADDOP_COMPARE(c, Is);
6893
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 RETURN_IF_FALSE(jump_to_fail_pop(c, pc, POP_JUMP_IF_FALSE));
6894 1 return 1;
6895 }
6896
6897 static int
6898 8 compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
6899 {
6900 8 SET_LOC(c, p);
6901
3/9
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
8 switch (p->kind) {
6902 case MatchValue_kind:
6903 return compiler_pattern_value(c, p, pc);
6904 1 case MatchSingleton_kind:
6905 1 return compiler_pattern_singleton(c, p, pc);
6906 case MatchSequence_kind:
6907 return compiler_pattern_sequence(c, p, pc);
6908 case MatchMapping_kind:
6909 return compiler_pattern_mapping(c, p, pc);
6910 4 case MatchClass_kind:
6911 4 return compiler_pattern_class(c, p, pc);
6912 case MatchStar_kind:
6913 return compiler_pattern_star(c, p, pc);
6914 3 case MatchAs_kind:
6915 3 return compiler_pattern_as(c, p, pc);
6916 case MatchOr_kind:
6917 return compiler_pattern_or(c, p, pc);
6918 }
6919 // AST validator shouldn't let this happen, but if it does,
6920 // just fail, don't crash out of the interpreter
6921 const char *e = "invalid match pattern node in AST (kind=%d)";
6922 return compiler_error(c, e, p->kind);
6923 }
6924
6925 static int
6926 3 compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
6927 {
6928
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 VISIT(c, expr, s->v.Match.subject);
6929 basicblock *end;
6930
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 RETURN_IF_FALSE(end = compiler_new_block(c));
6931
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
6932 assert(cases > 0);
6933 3 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6934
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
3 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
6935
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
6936 6 m = asdl_seq_GET(s->v.Match.cases, i);
6937 6 SET_LOC(c, m->pattern);
6938 // Only copy the subject if we're *not* on the last case:
6939
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (i != cases - has_default - 1) {
6940
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 ADDOP_I(c, COPY, 1);
6941 }
6942
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 RETURN_IF_FALSE(pc->stores = PyList_New(0));
6943 // Irrefutable cases must be either guarded, last, or both:
6944
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
6 pc->allow_irrefutable = m->guard != NULL || i == cases - 1;
6945 6 pc->fail_pop = NULL;
6946 6 pc->fail_pop_size = 0;
6947 6 pc->on_top = 0;
6948 // NOTE: Can't use returning macros here (they'll leak pc->stores)!
6949
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 if (!compiler_pattern(c, m->pattern, pc)) {
6950 Py_DECREF(pc->stores);
6951 return 0;
6952 }
6953 assert(!pc->on_top);
6954 // It's a match! Store all of the captured names (they're on the stack).
6955 6 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
6956
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 for (Py_ssize_t n = 0; n < nstores; n++) {
6957 3 PyObject *name = PyList_GET_ITEM(pc->stores, n);
6958
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (!compiler_nameop(c, name, Store)) {
6959 Py_DECREF(pc->stores);
6960 return 0;
6961 }
6962 }
6963 6 Py_DECREF(pc->stores);
6964 // NOTE: Returning macros are safe again.
6965
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (m->guard) {
6966
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 RETURN_IF_FALSE(ensure_fail_pop(c, pc, 0));
6967
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, pc->fail_pop[0], 0));
6968 }
6969 // Success! Pop the subject off, we're done with it:
6970
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (i != cases - has_default - 1) {
6971
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 ADDOP(c, POP_TOP);
6972 }
6973
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 17 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 11 times.
✓ Branch 6 taken 6 times.
17 VISIT_SEQ(c, stmt, m->body);
6974
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 ADDOP_JUMP(c, JUMP, end);
6975 // If the pattern fails to match, we want the line number of the
6976 // cleanup to be associated with the failed pattern, not the last line
6977 // of the body
6978 6 SET_LOC(c, m->pattern);
6979
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 RETURN_IF_FALSE(emit_and_reset_fail_pop(c, pc));
6980 }
6981
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (has_default) {
6982 // A trailing "case _" is common, and lets us save a bit of redundant
6983 // pushing and popping in the loop above:
6984 1 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
6985 1 SET_LOC(c, m->pattern);
6986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (cases == 1) {
6987 // No matches. Done with the subject:
6988 ADDOP(c, POP_TOP);
6989 }
6990 else {
6991 // Show line coverage for default case (it doesn't create bytecode)
6992
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 ADDOP(c, NOP);
6993 }
6994
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (m->guard) {
6995 RETURN_IF_FALSE(compiler_jump_if(c, m->guard, end, 0));
6996 }
6997
4/6
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
2 VISIT_SEQ(c, stmt, m->body);
6998 }
6999 3 compiler_use_next_block(c, end);
7000 3 return 1;
7001 }
7002
7003 static int
7004 3 compiler_match(struct compiler *c, stmt_ty s)
7005 {
7006 pattern_context pc;
7007 3 pc.fail_pop = NULL;
7008 3 int result = compiler_match_inner(c, s, &pc);
7009 3 PyObject_Free(pc.fail_pop);
7010 3 return result;
7011 }
7012
7013 #undef WILDCARD_CHECK
7014 #undef WILDCARD_STAR_CHECK
7015
7016
7017 /* End of the compiler section, beginning of the assembler section */
7018
7019
7020 struct assembler {
7021 PyObject *a_bytecode; /* bytes containing bytecode */
7022 int a_offset; /* offset into bytecode */
7023 PyObject *a_except_table; /* bytes containing exception table */
7024 int a_except_table_off; /* offset into exception table */
7025 /* Location Info */
7026 int a_lineno; /* lineno of last emitted instruction */
7027 PyObject* a_linetable; /* bytes containing location info */
7028 int a_location_off; /* offset of last written location info frame */
7029 };
7030
7031 static basicblock**
7032 1991532 make_cfg_traversal_stack(basicblock *entryblock) {
7033 1991532 int nblocks = 0;
7034
2/2
✓ Branch 0 taken 6978899 times.
✓ Branch 1 taken 1991532 times.
8970431 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7035 6978899 b->b_visited = 0;
7036 6978899 nblocks++;
7037 }
7038 1991532 basicblock **stack = (basicblock **)PyMem_Malloc(sizeof(basicblock *) * nblocks);
7039
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1991532 times.
1991532 if (!stack) {
7040 PyErr_NoMemory();
7041 }
7042 1991532 return stack;
7043 }
7044
7045 Py_LOCAL_INLINE(void)
7046 1432499 stackdepth_push(basicblock ***sp, basicblock *b, int depth)
7047 {
7048 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
7049
3/4
✓ Branch 0 taken 1262784 times.
✓ Branch 1 taken 169715 times.
✓ Branch 2 taken 1262784 times.
✗ Branch 3 not taken.
1432499 if (b->b_startdepth < depth && b->b_startdepth < 100) {
7050 assert(b->b_startdepth < 0);
7051 1262784 b->b_startdepth = depth;
7052 1262784 *(*sp)++ = b;
7053 }
7054 1432499 }
7055
7056 /* Find the flow path that needs the largest stack. We assume that
7057 * cycles in the flow graph have no net effect on the stack depth.
7058 */
7059 static int
7060 448800 stackdepth(basicblock *entryblock, int code_flags)
7061 {
7062
2/2
✓ Branch 0 taken 1262784 times.
✓ Branch 1 taken 448800 times.
1711584 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7063 1262784 b->b_startdepth = INT_MIN;
7064 }
7065 448800 basicblock **stack = make_cfg_traversal_stack(entryblock);
7066
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (!stack) {
7067 return -1;
7068 }
7069
7070 448800 int maxdepth = 0;
7071 448800 basicblock **sp = stack;
7072
2/2
✓ Branch 0 taken 9384 times.
✓ Branch 1 taken 439416 times.
448800 if (code_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
7073 9384 stackdepth_push(&sp, entryblock, 1);
7074 } else {
7075 439416 stackdepth_push(&sp, entryblock, 0);
7076 }
7077
7078
2/2
✓ Branch 0 taken 1262784 times.
✓ Branch 1 taken 448800 times.
1711584 while (sp != stack) {
7079 1262784 basicblock *b = *--sp;
7080 1262784 int depth = b->b_startdepth;
7081 assert(depth >= 0);
7082 1262784 basicblock *next = b->b_next;
7083
2/2
✓ Branch 0 taken 14562721 times.
✓ Branch 1 taken 513193 times.
15075914 for (int i = 0; i < b->b_iused; i++) {
7084 14562721 struct instr *instr = &b->b_instr[i];
7085 14562721 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
7086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14562721 times.
14562721 if (effect == PY_INVALID_STACK_EFFECT) {
7087 PyErr_Format(PyExc_SystemError,
7088 "compiler stack_effect(opcode=%d, arg=%i) failed",
7089 instr->i_opcode, instr->i_oparg);
7090 return -1;
7091 }
7092 14562721 int new_depth = depth + effect;
7093
2/2
✓ Branch 0 taken 1786487 times.
✓ Branch 1 taken 12776234 times.
14562721 if (new_depth > maxdepth) {
7094 1786487 maxdepth = new_depth;
7095 }
7096 assert(depth >= 0); /* invalid code or bug in stackdepth() */
7097
4/4
✓ Branch 1 taken 14129690 times.
✓ Branch 2 taken 433031 times.
✓ Branch 4 taken 37475 times.
✓ Branch 5 taken 14092215 times.
14562721 if (is_jump(instr) || is_block_push(instr)) {
7098 470506 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
7099 assert(effect != PY_INVALID_STACK_EFFECT);
7100 470506 int target_depth = depth + effect;
7101
2/2
✓ Branch 0 taken 5229 times.
✓ Branch 1 taken 465277 times.
470506 if (target_depth > maxdepth) {
7102 5229 maxdepth = target_depth;
7103 }
7104 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
7105 470506 stackdepth_push(&sp, instr->i_target, target_depth);
7106 }
7107 14562721 depth = new_depth;
7108 assert(!IS_ASSEMBLER_OPCODE(instr->i_opcode));
7109
7/10
✓ Branch 0 taken 14466949 times.
✓ Branch 1 taken 95772 times.
✓ Branch 2 taken 14465291 times.
✓ Branch 3 taken 1658 times.
✓ Branch 4 taken 14465291 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 14465291 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14465291 times.
✗ Branch 9 not taken.
14562721 if (IS_UNCONDITIONAL_JUMP_OPCODE(instr->i_opcode) ||
7110
6/6
✓ Branch 0 taken 13950610 times.
✓ Branch 1 taken 514681 times.
✓ Branch 2 taken 13850593 times.
✓ Branch 3 taken 100017 times.
✓ Branch 4 taken 37463 times.
✓ Branch 5 taken 13813130 times.
14465291 IS_SCOPE_EXIT_OPCODE(instr->i_opcode))
7111 {
7112 /* remaining code is dead */
7113 749591 next = NULL;
7114 749591 break;
7115 }
7116
2/2
✓ Branch 0 taken 11907 times.
✓ Branch 1 taken 13801223 times.
13813130 if (instr->i_opcode == YIELD_VALUE) {
7117 11907 instr->i_oparg = depth;
7118 }
7119 }
7120
2/2
✓ Branch 0 taken 513193 times.
✓ Branch 1 taken 749591 times.
1262784 if (next != NULL) {
7121 assert(BB_HAS_FALLTHROUGH(b));
7122 513193 stackdepth_push(&sp, next, depth);
7123 }
7124 }
7125 448800 PyMem_Free(stack);
7126 448800 return maxdepth;
7127 }
7128
7129 static int
7130 448800 assemble_init(struct assembler *a, int firstlineno)
7131 {
7132 448800 memset(a, 0, sizeof(struct assembler));
7133 448800 a->a_lineno = firstlineno;
7134 448800 a->a_linetable = NULL;
7135 448800 a->a_location_off = 0;
7136 448800 a->a_except_table = NULL;
7137 448800 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
7138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (a->a_bytecode == NULL) {
7139 goto error;
7140 }
7141 448800 a->a_linetable = PyBytes_FromStringAndSize(NULL, DEFAULT_CNOTAB_SIZE);
7142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (a->a_linetable == NULL) {
7143 goto error;
7144 }
7145 448800 a->a_except_table = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
7146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (a->a_except_table == NULL) {
7147 goto error;
7148 }
7149 448800 return 1;
7150 error:
7151 Py_XDECREF(a->a_bytecode);
7152 Py_XDECREF(a->a_linetable);
7153 Py_XDECREF(a->a_except_table);
7154 return 0;
7155 }
7156
7157 static void
7158 448800 assemble_free(struct assembler *a)
7159 {
7160 448800 Py_XDECREF(a->a_bytecode);
7161 448800 Py_XDECREF(a->a_linetable);
7162 448800 Py_XDECREF(a->a_except_table);
7163 448800 }
7164
7165 static int
7166 1412234 blocksize(basicblock *b)
7167 {
7168 int i;
7169 1412234 int size = 0;
7170
7171
2/2
✓ Branch 0 taken 17389967 times.
✓ Branch 1 taken 1412234 times.
18802201 for (i = 0; i < b->b_iused; i++) {
7172 17389967 size += instr_size(&b->b_instr[i]);
7173 }
7174 1412234 return size;
7175 }
7176
7177 static basicblock *
7178 37475 push_except_block(ExceptStack *stack, struct instr *setup) {
7179 assert(is_block_push(setup));
7180 37475 int opcode = setup->i_opcode;
7181 37475 basicblock * target = setup->i_target;
7182
4/4
✓ Branch 0 taken 33114 times.
✓ Branch 1 taken 4361 times.
✓ Branch 2 taken 19751 times.
✓ Branch 3 taken 13363 times.
37475 if (opcode == SETUP_WITH || opcode == SETUP_CLEANUP) {
7183 24112 target->b_preserve_lasti = 1;
7184 }
7185 37475 stack->handlers[++stack->depth] = target;
7186 37475 return target;
7187 }
7188
7189 static basicblock *
7190 33655 pop_except_block(ExceptStack *stack) {
7191 assert(stack->depth > 0);
7192 33655 return stack->handlers[--stack->depth];
7193 }
7194
7195 static basicblock *
7196 1262784 except_stack_top(ExceptStack *stack) {
7197 1262784 return stack->handlers[stack->depth];
7198 }
7199
7200 static ExceptStack *
7201 448800 make_except_stack(void) {
7202 448800 ExceptStack *new = PyMem_Malloc(sizeof(ExceptStack));
7203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (new == NULL) {
7204 PyErr_NoMemory();
7205 return NULL;
7206 }
7207 448800 new->depth = 0;
7208 448800 new->handlers[0] = NULL;
7209 448800 return new;
7210 }
7211
7212 static ExceptStack *
7213 333470 copy_except_stack(ExceptStack *stack) {
7214 333470 ExceptStack *copy = PyMem_Malloc(sizeof(ExceptStack));
7215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 333470 times.
333470 if (copy == NULL) {
7216 PyErr_NoMemory();
7217 return NULL;
7218 }
7219 333470 memcpy(copy, stack, sizeof(ExceptStack));
7220 333470 return copy;
7221 }
7222
7223 static int
7224 448800 label_exception_targets(basicblock *entryblock) {
7225 448800 basicblock **todo_stack = make_cfg_traversal_stack(entryblock);
7226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (todo_stack == NULL) {
7227 return -1;
7228 }
7229 448800 ExceptStack *except_stack = make_except_stack();
7230
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (except_stack == NULL) {
7231 PyMem_Free(todo_stack);
7232 PyErr_NoMemory();
7233 return -1;
7234 }
7235 448800 except_stack->depth = 0;
7236 448800 todo_stack[0] = entryblock;
7237 448800 entryblock->b_visited = 1;
7238 448800 entryblock->b_exceptstack = except_stack;
7239 448800 basicblock **todo = &todo_stack[1];
7240 448800 basicblock *handler = NULL;
7241
2/2
✓ Branch 0 taken 1262784 times.
✓ Branch 1 taken 448800 times.
1711584 while (todo > todo_stack) {
7242 1262784 todo--;
7243 1262784 basicblock *b = todo[0];
7244 assert(b->b_visited == 1);
7245 1262784 except_stack = b->b_exceptstack;
7246 assert(except_stack != NULL);
7247 1262784 b->b_exceptstack = NULL;
7248 1262784 handler = except_stack_top(except_stack);
7249
2/2
✓ Branch 0 taken 14562721 times.
✓ Branch 1 taken 1262784 times.
15825505 for (int i = 0; i < b->b_iused; i++) {
7250 14562721 struct instr *instr = &b->b_instr[i];
7251
2/2
✓ Branch 1 taken 37475 times.
✓ Branch 2 taken 14525246 times.
14562721 if (is_block_push(instr)) {
7252
1/2
✓ Branch 0 taken 37475 times.
✗ Branch 1 not taken.
37475 if (!instr->i_target->b_visited) {
7253 37475 ExceptStack *copy = copy_except_stack(except_stack);
7254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37475 times.
37475 if (copy == NULL) {
7255 goto error;
7256 }
7257 37475 instr->i_target->b_exceptstack = copy;
7258 37475 todo[0] = instr->i_target;
7259 37475 instr->i_target->b_visited = 1;
7260 37475 todo++;
7261 }
7262 37475 handler = push_except_block(except_stack, instr);
7263 }
7264
2/2
✓ Branch 0 taken 33655 times.
✓ Branch 1 taken 14491591 times.
14525246 else if (instr->i_opcode == POP_BLOCK) {
7265 33655 handler = pop_except_block(except_stack);
7266 }
7267
2/2
✓ Branch 1 taken 433031 times.
✓ Branch 2 taken 14058560 times.
14491591 else if (is_jump(instr)) {
7268 433031 instr->i_except = handler;
7269 assert(i == b->b_iused -1);
7270
2/2
✓ Branch 0 taken 321960 times.
✓ Branch 1 taken 111071 times.
433031 if (!instr->i_target->b_visited) {
7271
2/2
✓ Branch 1 taken 295995 times.
✓ Branch 2 taken 25965 times.
321960 if (BB_HAS_FALLTHROUGH(b)) {
7272 295995 ExceptStack *copy = copy_except_stack(except_stack);
7273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 295995 times.
295995 if (copy == NULL) {
7274 goto error;
7275 }
7276 295995 instr->i_target->b_exceptstack = copy;
7277 }
7278 else {
7279 25965 instr->i_target->b_exceptstack = except_stack;
7280 25965 except_stack = NULL;
7281 }
7282 321960 todo[0] = instr->i_target;
7283 321960 instr->i_target->b_visited = 1;
7284 321960 todo++;
7285 }
7286 }
7287 else {
7288 14058560 instr->i_except = handler;
7289 }
7290 }
7291
4/4
✓ Branch 1 taken 513193 times.
✓ Branch 2 taken 749591 times.
✓ Branch 3 taken 454549 times.
✓ Branch 4 taken 58644 times.
1262784 if (BB_HAS_FALLTHROUGH(b) && !b->b_next->b_visited) {
7292 assert(except_stack != NULL);
7293 454549 b->b_next->b_exceptstack = except_stack;
7294 454549 todo[0] = b->b_next;
7295 454549 b->b_next->b_visited = 1;
7296 454549 todo++;
7297 }
7298
2/2
✓ Branch 0 taken 782270 times.
✓ Branch 1 taken 25965 times.
808235 else if (except_stack != NULL) {
7299 782270 PyMem_Free(except_stack);
7300 }
7301 }
7302 #ifdef Py_DEBUG
7303 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7304 assert(b->b_exceptstack == NULL);
7305 }
7306 #endif
7307 448800 PyMem_Free(todo_stack);
7308 448800 return 0;
7309 error:
7310 PyMem_Free(todo_stack);
7311 PyMem_Free(except_stack);
7312 return -1;
7313 }
7314
7315 static int
7316 98166 mark_warm(basicblock *entryblock) {
7317 98166 basicblock **stack = make_cfg_traversal_stack(entryblock);
7318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98166 times.
98166 if (stack == NULL) {
7319 return -1;
7320 }
7321 98166 basicblock **sp = stack;
7322
7323 98166 *sp++ = entryblock;
7324 98166 entryblock->b_visited = 1;
7325
2/2
✓ Branch 0 taken 823007 times.
✓ Branch 1 taken 98166 times.
921173 while (sp > stack) {
7326 823007 basicblock *b = *(--sp);
7327 assert(!b->b_except_predecessors);
7328 823007 b->b_warm = 1;
7329 823007 basicblock *next = b->b_next;
7330
6/6
✓ Branch 0 taken 730882 times.
✓ Branch 1 taken 92125 times.
✓ Branch 3 taken 480286 times.
✓ Branch 4 taken 250596 times.
✓ Branch 5 taken 436793 times.
✓ Branch 6 taken 43493 times.
823007 if (next && BB_HAS_FALLTHROUGH(b) && !next->b_visited) {
7331 436793 *sp++ = next;
7332 436793 next->b_visited = 1;
7333 }
7334
2/2
✓ Branch 0 taken 8640465 times.
✓ Branch 1 taken 823007 times.
9463472 for (int i=0; i < b->b_iused; i++) {
7335 8640465 struct instr *instr = &b->b_instr[i];
7336
4/4
✓ Branch 1 taken 405350 times.
✓ Branch 2 taken 8235115 times.
✓ Branch 3 taken 288048 times.
✓ Branch 4 taken 117302 times.
8640465 if (is_jump(instr) && !instr->i_target->b_visited) {
7337 288048 *sp++ = instr->i_target;
7338 288048 instr->i_target->b_visited = 1;
7339 }
7340 }
7341 }
7342 98166 PyMem_Free(stack);
7343 98166 return 0;
7344 }
7345
7346 static int
7347 98166 mark_cold(basicblock *entryblock) {
7348
2/2
✓ Branch 0 taken 912150 times.
✓ Branch 1 taken 98166 times.
1010316 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7349 assert(!b->b_cold && !b->b_warm);
7350 }
7351
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 98166 times.
98166 if (mark_warm(entryblock) < 0) {
7352 return -1;
7353 }
7354
7355 98166 basicblock **stack = make_cfg_traversal_stack(entryblock);
7356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98166 times.
98166 if (stack == NULL) {
7357 return -1;
7358 }
7359
7360 98166 basicblock **sp = stack;
7361
2/2
✓ Branch 0 taken 912150 times.
✓ Branch 1 taken 98166 times.
1010316 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7362
2/2
✓ Branch 0 taken 37475 times.
✓ Branch 1 taken 874675 times.
912150 if (b->b_except_predecessors) {
7363 assert(b->b_except_predecessors == b->b_predecessors);
7364 assert(!b->b_warm);
7365 37475 *sp++ = b;
7366 37475 b->b_visited = 1;
7367 }
7368 }
7369
7370
2/2
✓ Branch 0 taken 89143 times.
✓ Branch 1 taken 98166 times.
187309 while (sp > stack) {
7371 89143 basicblock *b = *(--sp);
7372 89143 b->b_cold = 1;
7373 89143 basicblock *next = b->b_next;
7374
4/4
✓ Branch 0 taken 83102 times.
✓ Branch 1 taken 6041 times.
✓ Branch 3 taken 32907 times.
✓ Branch 4 taken 50195 times.
89143 if (next && BB_HAS_FALLTHROUGH(b)) {
7375
4/4
✓ Branch 0 taken 32885 times.
✓ Branch 1 taken 22 times.
✓ Branch 2 taken 32181 times.
✓ Branch 3 taken 704 times.
32907 if (!next->b_warm && !next->b_visited) {
7376 32181 *sp++ = next;
7377 32181 next->b_visited = 1;
7378 }
7379 }
7380
2/2
✓ Branch 0 taken 340086 times.
✓ Branch 1 taken 89143 times.
429229 for (int i = 0; i < b->b_iused; i++) {
7381 340086 struct instr *instr = &b->b_instr[i];
7382
2/2
✓ Branch 1 taken 27681 times.
✓ Branch 2 taken 312405 times.
340086 if (is_jump(instr)) {
7383 assert(i == b->b_iused-1);
7384 27681 basicblock *target = b->b_instr[i].i_target;
7385
4/4
✓ Branch 0 taken 20596 times.
✓ Branch 1 taken 7085 times.
✓ Branch 2 taken 19487 times.
✓ Branch 3 taken 1109 times.
27681 if (!target->b_warm && !target->b_visited) {
7386 19487 *sp++ = target;
7387 19487 target->b_visited = 1;
7388 }
7389 }
7390 }
7391 }
7392 98166 PyMem_Free(stack);
7393 98166 return 0;
7394 }
7395
7396 static int
7397 448800 push_cold_blocks_to_end(basicblock *entryblock, int code_flags) {
7398
2/2
✓ Branch 0 taken 350634 times.
✓ Branch 1 taken 98166 times.
448800 if (entryblock->b_next == NULL) {
7399 /* single basicblock, no need to reorder */
7400 350634 return 0;
7401 }
7402
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 98166 times.
98166 if (mark_cold(entryblock) < 0) {
7403 return -1;
7404 }
7405
7406 /* If we have a cold block with fallthrough to a warm block, add */
7407 /* an explicit jump instead of fallthrough */
7408
2/2
✓ Branch 0 taken 912172 times.
✓ Branch 1 taken 98166 times.
1010338 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7409
7/8
✓ Branch 0 taken 89165 times.
✓ Branch 1 taken 823007 times.
✓ Branch 3 taken 32907 times.
✓ Branch 4 taken 56258 times.
✓ Branch 5 taken 32907 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 22 times.
✓ Branch 8 taken 32885 times.
912172 if (b->b_cold && BB_HAS_FALLTHROUGH(b) && b->b_next && b->b_next->b_warm) {
7410 22 basicblock *explicit_jump = basicblock_new_b_list_successor(b);
7411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
22 if (explicit_jump == NULL) {
7412 return -1;
7413 }
7414 22 basicblock_addop(explicit_jump, JUMP, 0, b->b_next, &NO_LOCATION);
7415
7416 22 explicit_jump->b_cold = 1;
7417 22 explicit_jump->b_next = b->b_next;
7418 22 b->b_next = explicit_jump;
7419 }
7420 }
7421
7422 assert(!entryblock->b_cold); /* First block can't be cold */
7423 98166 basicblock *cold_blocks = NULL;
7424 98166 basicblock *cold_blocks_tail = NULL;
7425
7426 98166 basicblock *b = entryblock;
7427
2/2
✓ Branch 0 taken 108950 times.
✓ Branch 1 taken 6041 times.
114991 while(b->b_next) {
7428 assert(!b->b_cold);
7429
4/4
✓ Branch 0 taken 741666 times.
✓ Branch 1 taken 92125 times.
✓ Branch 2 taken 724841 times.
✓ Branch 3 taken 16825 times.
833791 while (b->b_next && !b->b_next->b_cold) {
7430 724841 b = b->b_next;
7431 }
7432
2/2
✓ Branch 0 taken 92125 times.
✓ Branch 1 taken 16825 times.
108950 if (b->b_next == NULL) {
7433 /* no more cold blocks */
7434 92125 break;
7435 }
7436
7437 /* b->b_next is the beginning of a cold streak */
7438 assert(!b->b_cold && b->b_next->b_cold);
7439
7440 16825 basicblock *b_end = b->b_next;
7441
4/4
✓ Branch 0 taken 83124 times.
✓ Branch 1 taken 6041 times.
✓ Branch 2 taken 72340 times.
✓ Branch 3 taken 10784 times.
89165 while (b_end->b_next && b_end->b_next->b_cold) {
7442 72340 b_end = b_end->b_next;
7443 }
7444
7445 /* b_end is the end of the cold streak */
7446 assert(b_end && b_end->b_cold);
7447 assert(b_end->b_next == NULL || !b_end->b_next->b_cold);
7448
7449
2/2
✓ Branch 0 taken 13042 times.
✓ Branch 1 taken 3783 times.
16825 if (cold_blocks == NULL) {
7450 13042 cold_blocks = b->b_next;
7451 }
7452 else {
7453 3783 cold_blocks_tail->b_next = b->b_next;
7454 }
7455 16825 cold_blocks_tail = b_end;
7456 16825 b->b_next = b_end->b_next;
7457 16825 b_end->b_next = NULL;
7458 }
7459 assert(b != NULL && b->b_next == NULL);
7460 98166 b->b_next = cold_blocks;
7461 98166 return 0;
7462 }
7463
7464 static void
7465 448800 convert_exception_handlers_to_nops(basicblock *entryblock) {
7466
2/2
✓ Branch 0 taken 1262784 times.
✓ Branch 1 taken 448800 times.
1711584 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7467
2/2
✓ Branch 0 taken 14562721 times.
✓ Branch 1 taken 1262784 times.
15825505 for (int i = 0; i < b->b_iused; i++) {
7468 14562721 struct instr *instr = &b->b_instr[i];
7469
4/4
✓ Branch 1 taken 14525246 times.
✓ Branch 2 taken 37475 times.
✓ Branch 3 taken 33655 times.
✓ Branch 4 taken 14491591 times.
14562721 if (is_block_push(instr) || instr->i_opcode == POP_BLOCK) {
7470 71130 instr->i_opcode = NOP;
7471 }
7472 }
7473 }
7474 448800 }
7475
7476 static inline void
7477 300475 write_except_byte(struct assembler *a, int byte) {
7478 300475 unsigned char *p = (unsigned char *) PyBytes_AS_STRING(a->a_except_table);
7479 300475 p[a->a_except_table_off++] = byte;
7480 300475 }
7481
7482 #define CONTINUATION_BIT 64
7483
7484 static void
7485 211984 assemble_emit_exception_table_item(struct assembler *a, int value, int msb)
7486 {
7487 assert ((msb | 128) == 128);
7488 assert(value >= 0 && value < (1 << 30));
7489
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211984 times.
211984 if (value >= 1 << 24) {
7490 write_except_byte(a, (value >> 24) | CONTINUATION_BIT | msb);
7491 msb = 0;
7492 }
7493
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 211984 times.
211984 if (value >= 1 << 18) {
7494 write_except_byte(a, ((value >> 18)&0x3f) | CONTINUATION_BIT | msb);
7495 msb = 0;
7496 }
7497
2/2
✓ Branch 0 taken 329 times.
✓ Branch 1 taken 211655 times.
211984 if (value >= 1 << 12) {
7498 329 write_except_byte(a, ((value >> 12)&0x3f) | CONTINUATION_BIT | msb);
7499 329 msb = 0;
7500 }
7501
2/2
✓ Branch 0 taken 88162 times.
✓ Branch 1 taken 123822 times.
211984 if (value >= 1 << 6) {
7502 88162 write_except_byte(a, ((value >> 6)&0x3f) | CONTINUATION_BIT | msb);
7503 88162 msb = 0;
7504 }
7505 211984 write_except_byte(a, (value&0x3f) | msb);
7506 211984 }
7507
7508 /* See Objects/exception_handling_notes.txt for details of layout */
7509 #define MAX_SIZE_OF_ENTRY 20
7510
7511 static int
7512 52996 assemble_emit_exception_table_entry(struct assembler *a, int start, int end, basicblock *handler)
7513 {
7514 52996 Py_ssize_t len = PyBytes_GET_SIZE(a->a_except_table);
7515
2/2
✓ Branch 0 taken 20041 times.
✓ Branch 1 taken 32955 times.
52996 if (a->a_except_table_off + MAX_SIZE_OF_ENTRY >= len) {
7516
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20041 times.
20041 if (_PyBytes_Resize(&a->a_except_table, len * 2) < 0)
7517 return 0;
7518 }
7519 52996 int size = end-start;
7520 assert(end > start);
7521 52996 int target = handler->b_offset;
7522 52996 int depth = handler->b_startdepth - 1;
7523
2/2
✓ Branch 0 taken 37620 times.
✓ Branch 1 taken 15376 times.
52996 if (handler->b_preserve_lasti) {
7524 37620 depth -= 1;
7525 }
7526 assert(depth >= 0);
7527 52996 int depth_lasti = (depth<<1) | handler->b_preserve_lasti;
7528 52996 assemble_emit_exception_table_item(a, start, (1<<7));
7529 52996 assemble_emit_exception_table_item(a, size, 0);
7530 52996 assemble_emit_exception_table_item(a, target, 0);
7531 52996 assemble_emit_exception_table_item(a, depth_lasti, 0);
7532 52996 return 1;
7533 }
7534
7535 static int
7536 448800 assemble_exception_table(struct assembler *a, basicblock *entryblock)
7537 {
7538 basicblock *b;
7539 448800 int ioffset = 0;
7540 448800 basicblock *handler = NULL;
7541 448800 int start = -1;
7542
2/2
✓ Branch 0 taken 1262806 times.
✓ Branch 1 taken 448800 times.
1711606 for (b = entryblock; b != NULL; b = b->b_next) {
7543 1262806 ioffset = b->b_offset;
7544
2/2
✓ Branch 0 taken 14497150 times.
✓ Branch 1 taken 1262806 times.
15759956 for (int i = 0; i < b->b_iused; i++) {
7545 14497150 struct instr *instr = &b->b_instr[i];
7546
2/2
✓ Branch 0 taken 94663 times.
✓ Branch 1 taken 14402487 times.
14497150 if (instr->i_except != handler) {
7547
2/2
✓ Branch 0 taken 52996 times.
✓ Branch 1 taken 41667 times.
94663 if (handler != NULL) {
7548
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 52996 times.
52996 RETURN_IF_FALSE(assemble_emit_exception_table_entry(a, start, ioffset, handler));
7549 }
7550 94663 start = ioffset;
7551 94663 handler = instr->i_except;
7552 }
7553 14497150 ioffset += instr_size(instr);
7554 }
7555 }
7556
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (handler != NULL) {
7557 RETURN_IF_FALSE(assemble_emit_exception_table_entry(a, start, ioffset, handler));
7558 }
7559 448800 return 1;
7560 }
7561
7562 /* Code location emitting code. See locations.md for a description of the format. */
7563
7564 #define MSB 0x80
7565
7566 static void
7567 15111328 write_location_byte(struct assembler* a, int val)
7568 {
7569 15111328 PyBytes_AS_STRING(a->a_linetable)[a->a_location_off] = val&255;
7570 15111328 a->a_location_off++;
7571 15111328 }
7572
7573
7574 static uint8_t *
7575 33076852 location_pointer(struct assembler* a)
7576 {
7577 33076852 return (uint8_t *)PyBytes_AS_STRING(a->a_linetable) +
7578 33076852 a->a_location_off;
7579 }
7580
7581 static void
7582 15204649 write_location_first_byte(struct assembler* a, int code, int length)
7583 {
7584 15204649 a->a_location_off += write_location_entry_start(
7585 location_pointer(a), code, length);
7586 15204649 }
7587
7588 static void
7589 13389426 write_location_varint(struct assembler* a, unsigned int val)
7590 {
7591 13389426 uint8_t *ptr = location_pointer(a);
7592 13389426 a->a_location_off += write_varint(ptr, val);
7593 13389426 }
7594
7595
7596 static void
7597 4482777 write_location_signed_varint(struct assembler* a, int val)
7598 {
7599 4482777 uint8_t *ptr = location_pointer(a);
7600 4482777 a->a_location_off += write_signed_varint(ptr, val);
7601 4482777 }
7602
7603 static void
7604 5796020 write_location_info_short_form(struct assembler* a, int length, int column, int end_column)
7605 {
7606 assert(length > 0 && length <= 8);
7607 5796020 int column_low_bits = column & 7;
7608 5796020 int column_group = column >> 3;
7609 assert(column < 80);
7610 assert(end_column >= column);
7611 assert(end_column - column < 16);
7612 5796020 write_location_first_byte(a, PY_CODE_LOCATION_INFO_SHORT0 + column_group, length);
7613 5796020 write_location_byte(a, (column_low_bits << 4) | (end_column - column));
7614 5796020 }
7615
7616 static void
7617 4657654 write_location_info_oneline_form(struct assembler* a, int length, int line_delta, int column, int end_column)
7618 {
7619 assert(length > 0 && length <= 8);
7620 assert(line_delta >= 0 && line_delta < 3);
7621 assert(column < 128);
7622 assert(end_column < 128);
7623 4657654 write_location_first_byte(a, PY_CODE_LOCATION_INFO_ONE_LINE0 + line_delta, length);
7624 4657654 write_location_byte(a, column);
7625 4657654 write_location_byte(a, end_column);
7626 4657654 }
7627
7628 static void
7629 4463142 write_location_info_long_form(struct assembler* a, struct instr* i, int length)
7630 {
7631 assert(length > 0 && length <= 8);
7632 4463142 write_location_first_byte(a, PY_CODE_LOCATION_INFO_LONG, length);
7633 4463142 write_location_signed_varint(a, i->i_loc.lineno - a->a_lineno);
7634 assert(i->i_loc.end_lineno >= i->i_loc.lineno);
7635 4463142 write_location_varint(a, i->i_loc.end_lineno - i->i_loc.lineno);
7636 4463142 write_location_varint(a, i->i_loc.col_offset + 1);
7637 4463142 write_location_varint(a, i->i_loc.end_col_offset + 1);
7638 4463142 }
7639
7640 static void
7641 268198 write_location_info_none(struct assembler* a, int length)
7642 {
7643 268198 write_location_first_byte(a, PY_CODE_LOCATION_INFO_NONE, length);
7644 268198 }
7645
7646 static void
7647 19635 write_location_info_no_column(struct assembler* a, int length, int line_delta)
7648 {
7649 19635 write_location_first_byte(a, PY_CODE_LOCATION_INFO_NO_COLUMNS, length);
7650 19635 write_location_signed_varint(a, line_delta);
7651 19635 }
7652
7653 #define THEORETICAL_MAX_ENTRY_SIZE 25 /* 1 + 6 + 6 + 6 + 6 */
7654
7655 static int
7656 15204649 write_location_info_entry(struct assembler* a, struct instr* i, int isize)
7657 {
7658 15204649 Py_ssize_t len = PyBytes_GET_SIZE(a->a_linetable);
7659
2/2
✓ Branch 0 taken 713439 times.
✓ Branch 1 taken 14491210 times.
15204649 if (a->a_location_off + THEORETICAL_MAX_ENTRY_SIZE >= len) {
7660 assert(len > THEORETICAL_MAX_ENTRY_SIZE);
7661
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 713439 times.
713439 if (_PyBytes_Resize(&a->a_linetable, len*2) < 0) {
7662 return 0;
7663 }
7664 }
7665
2/2
✓ Branch 0 taken 268198 times.
✓ Branch 1 taken 14936451 times.
15204649 if (i->i_loc.lineno < 0) {
7666 268198 write_location_info_none(a, isize);
7667 268198 return 1;
7668 }
7669 14936451 int line_delta = i->i_loc.lineno - a->a_lineno;
7670 14936451 int column = i->i_loc.col_offset;
7671 14936451 int end_column = i->i_loc.end_col_offset;
7672 assert(column >= -1);
7673 assert(end_column >= -1);
7674
3/4
✓ Branch 0 taken 14916816 times.
✓ Branch 1 taken 19635 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14916816 times.
14936451 if (column < 0 || end_column < 0) {
7675
3/4
✓ Branch 0 taken 867 times.
✓ Branch 1 taken 18768 times.
✓ Branch 2 taken 867 times.
✗ Branch 3 not taken.
19635 if (i->i_loc.end_lineno == i->i_loc.lineno || i->i_loc.end_lineno == -1) {
7676 19635 write_location_info_no_column(a, isize, line_delta);
7677 19635 a->a_lineno = i->i_loc.lineno;
7678 19635 return 1;
7679 }
7680 }
7681
2/2
✓ Branch 0 taken 13264935 times.
✓ Branch 1 taken 1651881 times.
14916816 else if (i->i_loc.end_lineno == i->i_loc.lineno) {
7682
8/8
✓ Branch 0 taken 11848001 times.
✓ Branch 1 taken 1416934 times.
✓ Branch 2 taken 8970137 times.
✓ Branch 3 taken 2877864 times.
✓ Branch 4 taken 5796039 times.
✓ Branch 5 taken 3174098 times.
✓ Branch 6 taken 5796020 times.
✓ Branch 7 taken 19 times.
13264935 if (line_delta == 0 && column < 80 && end_column - column < 16 && end_column >= column) {
7683 5796020 write_location_info_short_form(a, isize, column, end_column);
7684 5796020 return 1;
7685 }
7686
8/8
✓ Branch 0 taken 7430932 times.
✓ Branch 1 taken 37983 times.
✓ Branch 2 taken 7135152 times.
✓ Branch 3 taken 295780 times.
✓ Branch 4 taken 5173641 times.
✓ Branch 5 taken 1961511 times.
✓ Branch 6 taken 4657654 times.
✓ Branch 7 taken 515987 times.
7468915 if (line_delta >= 0 && line_delta < 3 && column < 128 && end_column < 128) {
7687 4657654 write_location_info_oneline_form(a, isize, line_delta, column, end_column);
7688 4657654 a->a_lineno = i->i_loc.lineno;
7689 4657654 return 1;
7690 }
7691 }
7692 4463142 write_location_info_long_form(a, i, isize);
7693 4463142 a->a_lineno = i->i_loc.lineno;
7694 4463142 return 1;
7695 }
7696
7697 static int
7698 14497150 assemble_emit_location(struct assembler* a, struct instr* i)
7699 {
7700 14497150 int isize = instr_size(i);
7701
2/2
✓ Branch 0 taken 707499 times.
✓ Branch 1 taken 14497150 times.
15204649 while (isize > 8) {
7702
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 707499 times.
707499 if (!write_location_info_entry(a, i, 8)) {
7703 return 0;
7704 }
7705 707499 isize -= 8;
7706 }
7707 14497150 return write_location_info_entry(a, i, isize);
7708 }
7709
7710 /* assemble_emit()
7711 Extend the bytecode with a new instruction.
7712 Update lnotab if necessary.
7713 */
7714
7715 static int
7716 14497150 assemble_emit(struct assembler *a, struct instr *i)
7717 {
7718 14497150 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
7719 _Py_CODEUNIT *code;
7720
7721 14497150 int size = instr_size(i);
7722
2/2
✓ Branch 0 taken 177205 times.
✓ Branch 1 taken 14319945 times.
14497150 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
7723
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 177205 times.
177205 if (len > PY_SSIZE_T_MAX / 2)
7724 return 0;
7725
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 177205 times.
177205 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
7726 return 0;
7727 }
7728 14497150 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
7729 14497150 a->a_offset += size;
7730 14497150 write_instr(code, i, size);
7731 14497150 return 1;
7732 }
7733
7734 static void
7735 448800 normalize_jumps(basicblock *entryblock)
7736 {
7737
2/2
✓ Branch 0 taken 1262806 times.
✓ Branch 1 taken 448800 times.
1711606 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7738 1262806 b->b_visited = 0;
7739 }
7740
2/2
✓ Branch 0 taken 1262806 times.
✓ Branch 1 taken 448800 times.
1711606 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7741 1262806 b->b_visited = 1;
7742
2/2
✓ Branch 0 taken 368 times.
✓ Branch 1 taken 1262438 times.
1262806 if (b->b_iused == 0) {
7743 368 continue;
7744 }
7745 1262438 struct instr *last = &b->b_instr[b->b_iused-1];
7746 assert(!IS_ASSEMBLER_OPCODE(last->i_opcode));
7747
2/2
✓ Branch 1 taken 423956 times.
✓ Branch 2 taken 838482 times.
1262438 if (is_jump(last)) {
7748 423956 bool is_forward = last->i_target->b_visited == 0;
7749
8/8
✓ Branch 0 taken 86697 times.
✓ Branch 1 taken 1658 times.
✓ Branch 2 taken 8595 times.
✓ Branch 3 taken 11618 times.
✓ Branch 4 taken 144917 times.
✓ Branch 5 taken 105018 times.
✓ Branch 6 taken 13428 times.
✓ Branch 7 taken 52025 times.
423956 switch(last->i_opcode) {
7750 86697 case JUMP:
7751
2/2
✓ Branch 0 taken 25692 times.
✓ Branch 1 taken 61005 times.
86697 last->i_opcode = is_forward ? JUMP_FORWARD : JUMP_BACKWARD;
7752 86697 break;
7753 1658 case JUMP_NO_INTERRUPT:
7754 1658 last->i_opcode = is_forward ?
7755
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1658 times.
1658 JUMP_FORWARD : JUMP_BACKWARD_NO_INTERRUPT;
7756 1658 break;
7757 8595 case POP_JUMP_IF_NOT_NONE:
7758 8595 last->i_opcode = is_forward ?
7759
2/2
✓ Branch 0 taken 8354 times.
✓ Branch 1 taken 241 times.
8595 POP_JUMP_FORWARD_IF_NOT_NONE : POP_JUMP_BACKWARD_IF_NOT_NONE;
7760 8595 break;
7761 11618 case POP_JUMP_IF_NONE:
7762 11618 last->i_opcode = is_forward ?
7763
2/2
✓ Branch 0 taken 10911 times.
✓ Branch 1 taken 707 times.
11618 POP_JUMP_FORWARD_IF_NONE : POP_JUMP_BACKWARD_IF_NONE;
7764 11618 break;
7765 144917 case POP_JUMP_IF_FALSE:
7766 144917 last->i_opcode = is_forward ?
7767
2/2
✓ Branch 0 taken 134229 times.
✓ Branch 1 taken 10688 times.
144917 POP_JUMP_FORWARD_IF_FALSE : POP_JUMP_BACKWARD_IF_FALSE;
7768 144917 break;
7769 105018 case POP_JUMP_IF_TRUE:
7770 105018 last->i_opcode = is_forward ?
7771
2/2
✓ Branch 0 taken 99908 times.
✓ Branch 1 taken 5110 times.
105018 POP_JUMP_FORWARD_IF_TRUE : POP_JUMP_BACKWARD_IF_TRUE;
7772 105018 break;
7773 13428 case JUMP_IF_TRUE_OR_POP:
7774 case JUMP_IF_FALSE_OR_POP:
7775
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13428 times.
13428 if (!is_forward) {
7776 /* As far as we can tell, the compiler never emits
7777 * these jumps with a backwards target. If/when this
7778 * exception is raised, we have found a use case for
7779 * a backwards version of this jump (or to replace
7780 * it with the sequence (COPY 1, POP_JUMP_IF_T/F, POP)
7781 */
7782 PyErr_Format(PyExc_SystemError,
7783 "unexpected %s jumping backwards",
7784 last->i_opcode == JUMP_IF_TRUE_OR_POP ?
7785 "JUMP_IF_TRUE_OR_POP" : "JUMP_IF_FALSE_OR_POP");
7786 }
7787 13428 break;
7788 }
7789 }
7790 }
7791 448800 }
7792
7793 static void
7794 453660 assemble_jump_offsets(basicblock *entryblock)
7795 {
7796 int bsize, totsize, extended_arg_recompile;
7797
7798 /* Compute the size of each block and fixup jump args.
7799 Replace block pointer with position in bytecode. */
7800 do {
7801 453660 totsize = 0;
7802
2/2
✓ Branch 0 taken 1412234 times.
✓ Branch 1 taken 453660 times.
1865894 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7803 1412234 bsize = blocksize(b);
7804 1412234 b->b_offset = totsize;
7805 1412234 totsize += bsize;
7806 }
7807 453660 extended_arg_recompile = 0;
7808
2/2
✓ Branch 0 taken 1412234 times.
✓ Branch 1 taken 453660 times.
1865894 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7809 1412234 bsize = b->b_offset;
7810
2/2
✓ Branch 0 taken 17389967 times.
✓ Branch 1 taken 1412234 times.
18802201 for (int i = 0; i < b->b_iused; i++) {
7811 17389967 struct instr *instr = &b->b_instr[i];
7812 17389967 int isize = instr_size(instr);
7813 /* Relative jumps are computed relative to
7814 the instruction pointer after fetching
7815 the jump instruction.
7816 */
7817 17389967 bsize += isize;
7818
2/2
✓ Branch 1 taken 508695 times.
✓ Branch 2 taken 16881272 times.
17389967 if (is_jump(instr)) {
7819 508695 instr->i_oparg = instr->i_target->b_offset;
7820
1/2
✓ Branch 1 taken 508695 times.
✗ Branch 2 not taken.
508695 if (is_relative_jump(instr)) {
7821
2/2
✓ Branch 0 taken 96402 times.
✓ Branch 1 taken 412293 times.
508695 if (instr->i_oparg < bsize) {
7822 assert(IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode));
7823 96402 instr->i_oparg = bsize - instr->i_oparg;
7824 }
7825 else {
7826 assert(!IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode));
7827 412293 instr->i_oparg -= bsize;
7828 }
7829 }
7830 else {
7831 assert(!IS_BACKWARDS_JUMP_OPCODE(instr->i_opcode));
7832 }
7833
2/2
✓ Branch 1 taken 12181 times.
✓ Branch 2 taken 496514 times.
508695 if (instr_size(instr) != isize) {
7834 12181 extended_arg_recompile = 1;
7835 }
7836 }
7837 }
7838 }
7839
7840 /* XXX: This is an awful hack that could hurt performance, but
7841 on the bright side it should work until we come up
7842 with a better solution.
7843
7844 The issue is that in the first loop blocksize() is called
7845 which calls instr_size() which requires i_oparg be set
7846 appropriately. There is a bootstrap problem because
7847 i_oparg is calculated in the second loop above.
7848
7849 So we loop until we stop seeing new EXTENDED_ARGs.
7850 The only EXTENDED_ARGs that could be popping up are
7851 ones in jump instructions. So this should converge
7852 fairly quickly.
7853 */
7854
2/2
✓ Branch 0 taken 4860 times.
✓ Branch 1 taken 448800 times.
453660 } while (extended_arg_recompile);
7855 448800 }
7856
7857
7858 // Ensure each basicblock is only put onto the stack once.
7859 #define MAYBE_PUSH(B) do { \
7860 if ((B)->b_visited == 0) { \
7861 *(*stack_top)++ = (B); \
7862 (B)->b_visited = 1; \
7863 } \
7864 } while (0)
7865
7866 static void
7867 9620135 scan_block_for_local(int target, basicblock *b, bool unsafe_to_start,
7868 basicblock ***stack_top)
7869 {
7870 9620135 bool unsafe = unsafe_to_start;
7871
2/2
✓ Branch 0 taken 1775622210 times.
✓ Branch 1 taken 9620135 times.
1785242345 for (int i = 0; i < b->b_iused; i++) {
7872 1775622210 struct instr *instr = &b->b_instr[i];
7873 assert(instr->i_opcode != EXTENDED_ARG);
7874 assert(instr->i_opcode != EXTENDED_ARG_QUICK);
7875 assert(instr->i_opcode != LOAD_FAST__LOAD_FAST);
7876 assert(instr->i_opcode != STORE_FAST__LOAD_FAST);
7877 assert(instr->i_opcode != LOAD_CONST__LOAD_FAST);
7878 assert(instr->i_opcode != STORE_FAST__STORE_FAST);
7879 assert(instr->i_opcode != LOAD_FAST__LOAD_CONST);
7880
4/4
✓ Branch 0 taken 356649824 times.
✓ Branch 1 taken 1418972386 times.
✓ Branch 2 taken 1596583 times.
✓ Branch 3 taken 355053241 times.
1775622210 if (unsafe && instr->i_except != NULL) {
7881
2/2
✓ Branch 0 taken 101986 times.
✓ Branch 1 taken 1494597 times.
1596583 MAYBE_PUSH(instr->i_except);
7882 }
7883
2/2
✓ Branch 0 taken 1765878675 times.
✓ Branch 1 taken 9743535 times.
1775622210 if (instr->i_oparg != target) {
7884 1765878675 continue;
7885 }
7886
4/5
✗ Branch 0 not taken.
✓ Branch 1 taken 2643931 times.
✓ Branch 2 taken 879267 times.
✓ Branch 3 taken 3401 times.
✓ Branch 4 taken 6216936 times.
9743535 switch (instr->i_opcode) {
7887 case LOAD_FAST_CHECK:
7888 // if this doesn't raise, then var is defined
7889 unsafe = false;
7890 break;
7891 2643931 case LOAD_FAST:
7892
2/2
✓ Branch 0 taken 2759 times.
✓ Branch 1 taken 2641172 times.
2643931 if (unsafe) {
7893 2759 instr->i_opcode = LOAD_FAST_CHECK;
7894 }
7895 2643931 unsafe = false;
7896 2643931 break;
7897 879267 case STORE_FAST:
7898 879267 unsafe = false;
7899 879267 break;
7900 3401 case DELETE_FAST:
7901 3401 unsafe = true;
7902 3401 break;
7903 }
7904 }
7905
2/2
✓ Branch 0 taken 2135144 times.
✓ Branch 1 taken 7484991 times.
9620135 if (unsafe) {
7906 // unsafe at end of this block,
7907 // so unsafe at start of next blocks
7908
4/4
✓ Branch 0 taken 2029748 times.
✓ Branch 1 taken 105396 times.
✓ Branch 3 taken 1421646 times.
✓ Branch 4 taken 608102 times.
2135144 if (b->b_next && BB_HAS_FALLTHROUGH(b)) {
7909
2/2
✓ Branch 0 taken 1259391 times.
✓ Branch 1 taken 162255 times.
1421646 MAYBE_PUSH(b->b_next);
7910 }
7911
2/2
✓ Branch 0 taken 2133400 times.
✓ Branch 1 taken 1744 times.
2135144 if (b->b_iused > 0) {
7912 2133400 struct instr *last = &b->b_instr[b->b_iused-1];
7913
2/2
✓ Branch 1 taken 1143123 times.
✓ Branch 2 taken 990277 times.
2133400 if (is_jump(last)) {
7914 assert(last->i_target != NULL);
7915
2/2
✓ Branch 0 taken 803178 times.
✓ Branch 1 taken 339945 times.
1143123 MAYBE_PUSH(last->i_target);
7916 }
7917 }
7918 }
7919 9620135 }
7920 #undef MAYBE_PUSH
7921
7922 static int
7923 448800 add_checks_for_loads_of_unknown_variables(basicblock *entryblock,
7924 struct compiler *c)
7925 {
7926 448800 basicblock **stack = make_cfg_traversal_stack(entryblock);
7927
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (stack == NULL) {
7928 return -1;
7929 }
7930 448800 Py_ssize_t nparams = PyList_GET_SIZE(c->u->u_ste->ste_varnames);
7931 448800 int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
7932
2/2
✓ Branch 0 taken 1258630 times.
✓ Branch 1 taken 448800 times.
1707430 for (int target = 0; target < nlocals; target++) {
7933
2/2
✓ Branch 0 taken 7170880 times.
✓ Branch 1 taken 1258630 times.
8429510 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7934 7170880 b->b_visited = 0;
7935 }
7936 1258630 basicblock **stack_top = stack;
7937
7938 // First pass: find the relevant DFS starting points:
7939 // the places where "being uninitialized" originates,
7940 // which are the entry block and any DELETE_FAST statements.
7941
2/2
✓ Branch 0 taken 284700 times.
✓ Branch 1 taken 973930 times.
1258630 if (target >= nparams) {
7942 // only non-parameter locals start out uninitialized.
7943 284700 *(stack_top++) = entryblock;
7944 284700 entryblock->b_visited = 1;
7945 }
7946
2/2
✓ Branch 0 taken 7170880 times.
✓ Branch 1 taken 1258630 times.
8429510 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
7947 7170880 scan_block_for_local(target, b, false, &stack_top);
7948 }
7949
7950 // Second pass: Depth-first search to propagate uncertainty
7951
2/2
✓ Branch 0 taken 2449255 times.
✓ Branch 1 taken 1258630 times.
3707885 while (stack_top > stack) {
7952 2449255 basicblock *b = *--stack_top;
7953 2449255 scan_block_for_local(target, b, true, &stack_top);
7954 }
7955 }
7956 448800 PyMem_Free(stack);
7957 448800 return 0;
7958 }
7959
7960 static PyObject *
7961 448800 dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
7962 {
7963 PyObject *tuple, *k, *v;
7964 448800 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
7965
7966 448800 tuple = PyTuple_New(size);
7967
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (tuple == NULL)
7968 return NULL;
7969
2/2
✓ Branch 1 taken 1448588 times.
✓ Branch 2 taken 448800 times.
1897388 while (PyDict_Next(dict, &pos, &k, &v)) {
7970 1448588 i = PyLong_AS_LONG(v);
7971 1448588 Py_INCREF(k);
7972 assert((i - offset) < size);
7973 assert((i - offset) >= 0);
7974 1448588 PyTuple_SET_ITEM(tuple, i - offset, k);
7975 }
7976 448800 return tuple;
7977 }
7978
7979 static PyObject *
7980 448800 consts_dict_keys_inorder(PyObject *dict)
7981 {
7982 PyObject *consts, *k, *v;
7983 448800 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
7984
7985 448800 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
7986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (consts == NULL)
7987 return NULL;
7988
2/2
✓ Branch 1 taken 1367301 times.
✓ Branch 2 taken 448800 times.
1816101 while (PyDict_Next(dict, &pos, &k, &v)) {
7989 1367301 i = PyLong_AS_LONG(v);
7990 /* The keys of the dictionary can be tuples wrapping a constant.
7991 * (see compiler_add_o and _PyCode_ConstantKey). In that case
7992 * the object we want is always second. */
7993
2/2
✓ Branch 1 taken 144132 times.
✓ Branch 2 taken 1223169 times.
1367301 if (PyTuple_CheckExact(k)) {
7994 144132 k = PyTuple_GET_ITEM(k, 1);
7995 }
7996 1367301 Py_INCREF(k);
7997 assert(i < size);
7998 assert(i >= 0);
7999 1367301 PyList_SET_ITEM(consts, i, k);
8000 }
8001 448800 return consts;
8002 }
8003
8004 static int
8005 448800 compute_code_flags(struct compiler *c)
8006 {
8007 448800 PySTEntryObject *ste = c->u->u_ste;
8008 448800 int flags = 0;
8009
2/2
✓ Branch 0 taken 276911 times.
✓ Branch 1 taken 171889 times.
448800 if (ste->ste_type == FunctionBlock) {
8010 276911 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
8011
2/2
✓ Branch 0 taken 36500 times.
✓ Branch 1 taken 240411 times.
276911 if (ste->ste_nested)
8012 36500 flags |= CO_NESTED;
8013
4/4
✓ Branch 0 taken 8286 times.
✓ Branch 1 taken 268625 times.
✓ Branch 2 taken 8278 times.
✓ Branch 3 taken 8 times.
276911 if (ste->ste_generator && !ste->ste_coroutine)
8014 8278 flags |= CO_GENERATOR;
8015
4/4
✓ Branch 0 taken 268625 times.
✓ Branch 1 taken 8286 times.
✓ Branch 2 taken 1098 times.
✓ Branch 3 taken 267527 times.
276911 if (!ste->ste_generator && ste->ste_coroutine)
8016 1098 flags |= CO_COROUTINE;
8017
4/4
✓ Branch 0 taken 8286 times.
✓ Branch 1 taken 268625 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 8278 times.
276911 if (ste->ste_generator && ste->ste_coroutine)
8018 8 flags |= CO_ASYNC_GENERATOR;
8019
2/2
✓ Branch 0 taken 7353 times.
✓ Branch 1 taken 269558 times.
276911 if (ste->ste_varargs)
8020 7353 flags |= CO_VARARGS;
8021
2/2
✓ Branch 0 taken 9936 times.
✓ Branch 1 taken 266975 times.
276911 if (ste->ste_varkeywords)
8022 9936 flags |= CO_VARKEYWORDS;
8023 }
8024
8025 /* (Only) inherit compilerflags in PyCF_MASK */
8026 448800 flags |= (c->c_flags->cf_flags & PyCF_MASK);
8027
8028
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
448800 if ((IS_TOP_LEVEL_AWAIT(c)) &&
8029 ste->ste_coroutine &&
8030 !ste->ste_generator) {
8031 flags |= CO_COROUTINE;
8032 }
8033
8034 448800 return flags;
8035 }
8036
8037 // Merge *obj* with constant cache.
8038 // Unlike merge_consts_recursive(), this function doesn't work recursively.
8039 static int
8040 2712165 merge_const_one(PyObject *const_cache, PyObject **obj)
8041 {
8042 2712165 PyDict_CheckExact(const_cache);
8043 2712165 PyObject *key = _PyCode_ConstantKey(*obj);
8044
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2712165 times.
2712165 if (key == NULL) {
8045 return 0;
8046 }
8047
8048 // t is borrowed reference
8049 2712165 PyObject *t = PyDict_SetDefault(const_cache, key, key);
8050 2712165 Py_DECREF(key);
8051
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2712165 times.
2712165 if (t == NULL) {
8052 return 0;
8053 }
8054
2/2
✓ Branch 0 taken 2088122 times.
✓ Branch 1 taken 624043 times.
2712165 if (t == key) { // obj is new constant.
8055 2088122 return 1;
8056 }
8057
8058
1/2
✓ Branch 1 taken 624043 times.
✗ Branch 2 not taken.
624043 if (PyTuple_CheckExact(t)) {
8059 // t is still borrowed reference
8060 624043 t = PyTuple_GET_ITEM(t, 1);
8061 }
8062
8063 624043 Py_INCREF(t);
8064 624043 Py_DECREF(*obj);
8065 624043 *obj = t;
8066 624043 return 1;
8067 }
8068
8069 // This is in codeobject.c.
8070 extern void _Py_set_localsplus_info(int, PyObject *, unsigned char,
8071 PyObject *, PyObject *);
8072
8073 static void
8074 448800 compute_localsplus_info(struct compiler *c, int nlocalsplus,
8075 PyObject *names, PyObject *kinds)
8076 {
8077 PyObject *k, *v;
8078 448800 Py_ssize_t pos = 0;
8079
2/2
✓ Branch 1 taken 1258630 times.
✓ Branch 2 taken 448800 times.
1707430 while (PyDict_Next(c->u->u_varnames, &pos, &k, &v)) {
8080 1258630 int offset = (int)PyLong_AS_LONG(v);
8081 assert(offset >= 0);
8082 assert(offset < nlocalsplus);
8083 // For now we do not distinguish arg kinds.
8084 1258630 _PyLocals_Kind kind = CO_FAST_LOCAL;
8085
2/2
✓ Branch 1 taken 9373 times.
✓ Branch 2 taken 1249257 times.
1258630 if (PyDict_GetItem(c->u->u_cellvars, k) != NULL) {
8086 9373 kind |= CO_FAST_CELL;
8087 }
8088 1258630 _Py_set_localsplus_info(offset, k, kind, names, kinds);
8089 }
8090 448800 int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
8091
8092 // This counter mirrors the fix done in fix_cell_offsets().
8093 448800 int numdropped = 0;
8094 448800 pos = 0;
8095
2/2
✓ Branch 1 taken 20549 times.
✓ Branch 2 taken 448800 times.
469349 while (PyDict_Next(c->u->u_cellvars, &pos, &k, &v)) {
8096
2/2
✓ Branch 1 taken 9373 times.
✓ Branch 2 taken 11176 times.
20549 if (PyDict_GetItem(c->u->u_varnames, k) != NULL) {
8097 // Skip cells that are already covered by locals.
8098 9373 numdropped += 1;
8099 9373 continue;
8100 }
8101 11176 int offset = (int)PyLong_AS_LONG(v);
8102 assert(offset >= 0);
8103 11176 offset += nlocals - numdropped;
8104 assert(offset < nlocalsplus);
8105 11176 _Py_set_localsplus_info(offset, k, CO_FAST_CELL, names, kinds);
8106 }
8107
8108 448800 pos = 0;
8109
2/2
✓ Branch 1 taken 27419 times.
✓ Branch 2 taken 448800 times.
476219 while (PyDict_Next(c->u->u_freevars, &pos, &k, &v)) {
8110 27419 int offset = (int)PyLong_AS_LONG(v);
8111 assert(offset >= 0);
8112 27419 offset += nlocals - numdropped;
8113 assert(offset < nlocalsplus);
8114 27419 _Py_set_localsplus_info(offset, k, CO_FAST_FREE, names, kinds);
8115 }
8116 448800 }
8117
8118 static PyCodeObject *
8119 448800 makecode(struct compiler *c, struct assembler *a, PyObject *constslist,
8120 int maxdepth, int nlocalsplus, int code_flags)
8121 {
8122 448800 PyCodeObject *co = NULL;
8123 448800 PyObject *names = NULL;
8124 448800 PyObject *consts = NULL;
8125 448800 PyObject *localsplusnames = NULL;
8126 448800 PyObject *localspluskinds = NULL;
8127
8128 448800 names = dict_keys_inorder(c->u->u_names, 0);
8129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (!names) {
8130 goto error;
8131 }
8132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (!merge_const_one(c->c_const_cache, &names)) {
8133 goto error;
8134 }
8135
8136 448800 consts = PyList_AsTuple(constslist); /* PyCode_New requires a tuple */
8137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (consts == NULL) {
8138 goto error;
8139 }
8140
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (!merge_const_one(c->c_const_cache, &consts)) {
8141 goto error;
8142 }
8143
8144 assert(c->u->u_posonlyargcount < INT_MAX);
8145 assert(c->u->u_argcount < INT_MAX);
8146 assert(c->u->u_kwonlyargcount < INT_MAX);
8147 448800 int posonlyargcount = (int)c->u->u_posonlyargcount;
8148 448800 int posorkwargcount = (int)c->u->u_argcount;
8149 assert(INT_MAX - posonlyargcount - posorkwargcount > 0);
8150 448800 int kwonlyargcount = (int)c->u->u_kwonlyargcount;
8151
8152 448800 localsplusnames = PyTuple_New(nlocalsplus);
8153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (localsplusnames == NULL) {
8154 goto error;
8155 }
8156 448800 localspluskinds = PyBytes_FromStringAndSize(NULL, nlocalsplus);
8157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (localspluskinds == NULL) {
8158 goto error;
8159 }
8160 448800 compute_localsplus_info(c, nlocalsplus, localsplusnames, localspluskinds);
8161
8162 897600 struct _PyCodeConstructor con = {
8163 448800 .filename = c->c_filename,
8164 448800 .name = c->u->u_name,
8165
2/2
✓ Branch 0 taken 292115 times.
✓ Branch 1 taken 156685 times.
448800 .qualname = c->u->u_qualname ? c->u->u_qualname : c->u->u_name,
8166 .flags = code_flags,
8167
8168 448800 .code = a->a_bytecode,
8169 448800 .firstlineno = c->u->u_firstlineno,
8170 448800 .linetable = a->a_linetable,
8171
8172 .consts = consts,
8173 .names = names,
8174
8175 .localsplusnames = localsplusnames,
8176 .localspluskinds = localspluskinds,
8177
8178 448800 .argcount = posonlyargcount + posorkwargcount,
8179 .posonlyargcount = posonlyargcount,
8180 .kwonlyargcount = kwonlyargcount,
8181
8182 .stacksize = maxdepth,
8183
8184 448800 .exceptiontable = a->a_except_table,
8185 };
8186
8187
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (_PyCode_Validate(&con) < 0) {
8188 goto error;
8189 }
8190
8191
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (!merge_const_one(c->c_const_cache, &localsplusnames)) {
8192 goto error;
8193 }
8194 448800 con.localsplusnames = localsplusnames;
8195
8196 448800 co = _PyCode_New(&con);
8197
1/2
✓ Branch 0 taken 448800 times.
✗ Branch 1 not taken.
448800 if (co == NULL) {
8198 goto error;
8199 }
8200
8201 448800 error:
8202 448800 Py_XDECREF(names);
8203 448800 Py_XDECREF(consts);
8204 448800 Py_XDECREF(localsplusnames);
8205 448800 Py_XDECREF(localspluskinds);
8206 448800 return co;
8207 }
8208
8209
8210 /* For debugging purposes only */
8211 #if 0
8212 static void
8213 dump_instr(struct instr *i)
8214 {
8215 const char *jrel = (is_relative_jump(i)) ? "jrel " : "";
8216 const char *jabs = (is_jump(i) && !is_relative_jump(i))? "jabs " : "";
8217
8218 char arg[128];
8219
8220 *arg = '\0';
8221 if (HAS_ARG(i->i_opcode)) {
8222 sprintf(arg, "arg: %d ", i->i_oparg);
8223 }
8224 if (is_jump(i)) {
8225 sprintf(arg, "target: %p ", i->i_target);
8226 }
8227 if (is_block_push(i)) {
8228 sprintf(arg, "except_target: %p ", i->i_target);
8229 }
8230 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
8231 i->i_loc.lineno, i->i_opcode, arg, jabs, jrel);
8232 }
8233
8234 static void
8235 dump_basicblock(const basicblock *b)
8236 {
8237 const char *b_return = basicblock_returns(b) ? "return " : "";
8238 fprintf(stderr, "[%d %d %d %p] used: %d, depth: %d, offset: %d %s\n",
8239 b->b_cold, b->b_warm, BB_NO_FALLTHROUGH(b), b, b->b_iused,
8240 b->b_startdepth, b->b_offset, b_return);
8241 if (b->b_instr) {
8242 int i;
8243 for (i = 0; i < b->b_iused; i++) {
8244 fprintf(stderr, " [%02d] ", i);
8245 dump_instr(b->b_instr + i);
8246 }
8247 }
8248 }
8249 #endif
8250
8251
8252 static int
8253 normalize_basic_block(basicblock *bb);
8254
8255 static int
8256 optimize_cfg(basicblock *entryblock, PyObject *consts, PyObject *const_cache);
8257
8258 static int
8259 trim_unused_consts(basicblock *entryblock, PyObject *consts);
8260
8261 /* Duplicates exit BBs, so that line numbers can be propagated to them */
8262 static int
8263 duplicate_exits_without_lineno(basicblock *entryblock);
8264
8265 static int
8266 extend_block(basicblock *bb);
8267
8268 static int *
8269 448800 build_cellfixedoffsets(struct compiler *c)
8270 {
8271 448800 int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
8272 448800 int ncellvars = (int)PyDict_GET_SIZE(c->u->u_cellvars);
8273 448800 int nfreevars = (int)PyDict_GET_SIZE(c->u->u_freevars);
8274
8275 448800 int noffsets = ncellvars + nfreevars;
8276
1/2
✓ Branch 0 taken 448800 times.
✗ Branch 1 not taken.
448800 int *fixed = PyMem_New(int, noffsets);
8277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (fixed == NULL) {
8278 PyErr_NoMemory();
8279 return NULL;
8280 }
8281
2/2
✓ Branch 0 taken 47968 times.
✓ Branch 1 taken 448800 times.
496768 for (int i = 0; i < noffsets; i++) {
8282 47968 fixed[i] = nlocals + i;
8283 }
8284
8285 PyObject *varname, *cellindex;
8286 448800 Py_ssize_t pos = 0;
8287
2/2
✓ Branch 1 taken 20549 times.
✓ Branch 2 taken 448800 times.
469349 while (PyDict_Next(c->u->u_cellvars, &pos, &varname, &cellindex)) {
8288 20549 PyObject *varindex = PyDict_GetItem(c->u->u_varnames, varname);
8289
2/2
✓ Branch 0 taken 9373 times.
✓ Branch 1 taken 11176 times.
20549 if (varindex != NULL) {
8290 assert(PyLong_AS_LONG(cellindex) < INT_MAX);
8291 assert(PyLong_AS_LONG(varindex) < INT_MAX);
8292 9373 int oldindex = (int)PyLong_AS_LONG(cellindex);
8293 9373 int argoffset = (int)PyLong_AS_LONG(varindex);
8294 9373 fixed[oldindex] = argoffset;
8295 }
8296 }
8297
8298 448800 return fixed;
8299 }
8300
8301 static inline int
8302 57684 insert_instruction(basicblock *block, int pos, struct instr *instr) {
8303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 57684 times.
57684 if (basicblock_next_instr(block) < 0) {
8304 return -1;
8305 }
8306
2/2
✓ Branch 0 taken 965206 times.
✓ Branch 1 taken 57684 times.
1022890 for (int i = block->b_iused-1; i > pos; i--) {
8307 965206 block->b_instr[i] = block->b_instr[i-1];
8308 }
8309 57684 block->b_instr[pos] = *instr;
8310 57684 return 0;
8311 }
8312
8313 static int
8314 448800 insert_prefix_instructions(struct compiler *c, basicblock *entryblock,
8315 int *fixed, int nfreevars, int code_flags)
8316 {
8317 assert(c->u->u_firstlineno > 0);
8318
8319 /* Add the generator prefix instructions. */
8320
2/2
✓ Branch 0 taken 9384 times.
✓ Branch 1 taken 439416 times.
448800 if (code_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
8321 9384 struct instr make_gen = {
8322 .i_opcode = RETURN_GENERATOR,
8323 .i_oparg = 0,
8324 9384 .i_loc = LOCATION(c->u->u_firstlineno, c->u->u_firstlineno, -1, -1),
8325 .i_target = NULL,
8326 };
8327
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9384 times.
9384 if (insert_instruction(entryblock, 0, &make_gen) < 0) {
8328 return -1;
8329 }
8330 9384 struct instr pop_top = {
8331 .i_opcode = POP_TOP,
8332 .i_oparg = 0,
8333 .i_loc = NO_LOCATION,
8334 .i_target = NULL,
8335 };
8336
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9384 times.
9384 if (insert_instruction(entryblock, 1, &pop_top) < 0) {
8337 return -1;
8338 }
8339 }
8340
8341 /* Set up cells for any variable that escapes, to be put in a closure. */
8342 448800 const int ncellvars = (int)PyDict_GET_SIZE(c->u->u_cellvars);
8343
2/2
✓ Branch 0 taken 12091 times.
✓ Branch 1 taken 436709 times.
448800 if (ncellvars) {
8344 // c->u->u_cellvars has the cells out of order so we sort them
8345 // before adding the MAKE_CELL instructions. Note that we
8346 // adjust for arg cells, which come first.
8347 12091 const int nvars = ncellvars + (int)PyDict_GET_SIZE(c->u->u_varnames);
8348 12091 int *sorted = PyMem_RawCalloc(nvars, sizeof(int));
8349
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12091 times.
12091 if (sorted == NULL) {
8350 PyErr_NoMemory();
8351 return -1;
8352 }
8353
2/2
✓ Branch 0 taken 20549 times.
✓ Branch 1 taken 12091 times.
32640 for (int i = 0; i < ncellvars; i++) {
8354 20549 sorted[fixed[i]] = i + 1;
8355 }
8356
2/2
✓ Branch 0 taken 72358 times.
✓ Branch 1 taken 12091 times.
84449 for (int i = 0, ncellsused = 0; ncellsused < ncellvars; i++) {
8357 72358 int oldindex = sorted[i] - 1;
8358
2/2
✓ Branch 0 taken 51809 times.
✓ Branch 1 taken 20549 times.
72358 if (oldindex == -1) {
8359 51809 continue;
8360 }
8361 20549 struct instr make_cell = {
8362 .i_opcode = MAKE_CELL,
8363 // This will get fixed in offset_derefs().
8364 .i_oparg = oldindex,
8365 .i_loc = NO_LOCATION,
8366 .i_target = NULL,
8367 };
8368
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 20549 times.
20549 if (insert_instruction(entryblock, ncellsused, &make_cell) < 0) {
8369 return -1;
8370 }
8371 20549 ncellsused += 1;
8372 }
8373 12091 PyMem_RawFree(sorted);
8374 }
8375
8376
2/2
✓ Branch 0 taken 18367 times.
✓ Branch 1 taken 430433 times.
448800 if (nfreevars) {
8377 18367 struct instr copy_frees = {
8378 .i_opcode = COPY_FREE_VARS,
8379 .i_oparg = nfreevars,
8380 .i_loc = NO_LOCATION,
8381 .i_target = NULL,
8382 };
8383
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 18367 times.
18367 if (insert_instruction(entryblock, 0, &copy_frees) < 0) {
8384 return -1;
8385 }
8386
8387 }
8388
8389 448800 return 0;
8390 }
8391
8392 /* Make sure that all returns have a line number, even if early passes
8393 * have failed to propagate a correct line number.
8394 * The resulting line number may not be correct according to PEP 626,
8395 * but should be "good enough", and no worse than in older versions. */
8396 static void
8397 448800 guarantee_lineno_for_exits(basicblock *entryblock, int firstlineno) {
8398 448800 int lineno = firstlineno;
8399 assert(lineno > 0);
8400
2/2
✓ Branch 0 taken 1262784 times.
✓ Branch 1 taken 448800 times.
1711584 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
8401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1262784 times.
1262784 if (b->b_iused == 0) {
8402 continue;
8403 }
8404 1262784 struct instr *last = &b->b_instr[b->b_iused-1];
8405
2/2
✓ Branch 0 taken 19788 times.
✓ Branch 1 taken 1242996 times.
1262784 if (last->i_loc.lineno < 0) {
8406
2/2
✓ Branch 0 taken 289 times.
✓ Branch 1 taken 19499 times.
19788 if (last->i_opcode == RETURN_VALUE) {
8407
2/2
✓ Branch 0 taken 867 times.
✓ Branch 1 taken 289 times.
1156 for (int i = 0; i < b->b_iused; i++) {
8408 assert(b->b_instr[i].i_loc.lineno < 0);
8409
8410 867 b->b_instr[i].i_loc.lineno = lineno;
8411 }
8412 }
8413 }
8414 else {
8415 1242996 lineno = last->i_loc.lineno;
8416 }
8417 }
8418 448800 }
8419
8420 static int
8421 448800 fix_cell_offsets(struct compiler *c, basicblock *entryblock, int *fixedmap)
8422 {
8423 448800 int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
8424 448800 int ncellvars = (int)PyDict_GET_SIZE(c->u->u_cellvars);
8425 448800 int nfreevars = (int)PyDict_GET_SIZE(c->u->u_freevars);
8426 448800 int noffsets = ncellvars + nfreevars;
8427
8428 // First deal with duplicates (arg cells).
8429 448800 int numdropped = 0;
8430
2/2
✓ Branch 0 taken 47968 times.
✓ Branch 1 taken 448800 times.
496768 for (int i = 0; i < noffsets ; i++) {
8431
2/2
✓ Branch 0 taken 38595 times.
✓ Branch 1 taken 9373 times.
47968 if (fixedmap[i] == i + nlocals) {
8432 38595 fixedmap[i] -= numdropped;
8433 }
8434 else {
8435 // It was a duplicate (cell/arg).
8436 9373 numdropped += 1;
8437 }
8438 }
8439
8440 // Then update offsets, either relative to locals or by cell2arg.
8441
2/2
✓ Branch 0 taken 1366225 times.
✓ Branch 1 taken 448800 times.
1815025 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
8442
2/2
✓ Branch 0 taken 15792134 times.
✓ Branch 1 taken 1366225 times.
17158359 for (int i = 0; i < b->b_iused; i++) {
8443 15792134 struct instr *inst = &b->b_instr[i];
8444 // This is called before extended args are generated.
8445 assert(inst->i_opcode != EXTENDED_ARG);
8446 assert(inst->i_opcode != EXTENDED_ARG_QUICK);
8447 15792134 int oldoffset = inst->i_oparg;
8448
2/2
✓ Branch 0 taken 137829 times.
✓ Branch 1 taken 15654305 times.
15792134 switch(inst->i_opcode) {
8449 137829 case MAKE_CELL:
8450 case LOAD_CLOSURE:
8451 case LOAD_DEREF:
8452 case STORE_DEREF:
8453 case DELETE_DEREF:
8454 case LOAD_CLASSDEREF:
8455 assert(oldoffset >= 0);
8456 assert(oldoffset < noffsets);
8457 assert(fixedmap[oldoffset] >= 0);
8458 137829 inst->i_oparg = fixedmap[oldoffset];
8459 }
8460 }
8461 }
8462
8463 448800 return numdropped;
8464 }
8465
8466 static void
8467 propagate_line_numbers(basicblock *entryblock);
8468
8469 static void
8470 eliminate_empty_basic_blocks(basicblock *entryblock);
8471
8472
8473 static void
8474 448800 remove_redundant_jumps(basicblock *entryblock) {
8475 /* If a non-empty block ends with a jump instruction, check if the next
8476 * non-empty block reached through normal flow control is the target
8477 * of that jump. If it is, then the jump instruction is redundant and
8478 * can be deleted.
8479 */
8480 448800 int removed = 0;
8481
2/2
✓ Branch 0 taken 1262806 times.
✓ Branch 1 taken 448800 times.
1711606 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
8482
1/2
✓ Branch 0 taken 1262806 times.
✗ Branch 1 not taken.
1262806 if (b->b_iused > 0) {
8483 1262806 struct instr *b_last_instr = &b->b_instr[b->b_iused - 1];
8484 assert(!IS_ASSEMBLER_OPCODE(b_last_instr->i_opcode));
8485
2/2
✓ Branch 0 taken 1167012 times.
✓ Branch 1 taken 95794 times.
1262806 if (b_last_instr->i_opcode == JUMP ||
8486
2/2
✓ Branch 0 taken 1658 times.
✓ Branch 1 taken 1165354 times.
1167012 b_last_instr->i_opcode == JUMP_NO_INTERRUPT) {
8487
2/2
✓ Branch 0 taken 9097 times.
✓ Branch 1 taken 88355 times.
97452 if (b_last_instr->i_target == b->b_next) {
8488 assert(b->b_next->b_iused);
8489 9097 b_last_instr->i_opcode = NOP;
8490 9097 removed++;
8491 }
8492 }
8493 }
8494 }
8495
2/2
✓ Branch 0 taken 7054 times.
✓ Branch 1 taken 441746 times.
448800 if (removed) {
8496 7054 eliminate_empty_basic_blocks(entryblock);
8497 }
8498 448800 }
8499
8500 static PyCodeObject *
8501 448800 assemble(struct compiler *c, int addNone)
8502 {
8503 448800 PyCodeObject *co = NULL;
8504 448800 PyObject *consts = NULL;
8505
8506 448800 int code_flags = compute_code_flags(c);
8507
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (code_flags < 0) {
8508 return NULL;
8509 }
8510
8511 /* Make sure every block that falls off the end returns None. */
8512
2/2
✓ Branch 1 taken 223810 times.
✓ Branch 2 taken 224990 times.
448800 if (!basicblock_returns(c->u->u_curblock)) {
8513 223810 UNSET_LOC(c);
8514
2/2
✓ Branch 0 taken 107793 times.
✓ Branch 1 taken 116017 times.
223810 if (addNone)
8515
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 107793 times.
107793 ADDOP_LOAD_CONST(c, Py_None);
8516
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 223810 times.
223810 ADDOP(c, RETURN_VALUE);
8517 }
8518
8519
2/2
✓ Branch 0 taken 1366225 times.
✓ Branch 1 taken 448800 times.
1815025 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
8520
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1366225 times.
1366225 if (normalize_basic_block(b)) {
8521 return NULL;
8522 }
8523 }
8524
8525
2/2
✓ Branch 0 taken 1366225 times.
✓ Branch 1 taken 448800 times.
1815025 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
8526
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1366225 times.
1366225 if (extend_block(b)) {
8527 return NULL;
8528 }
8529 }
8530
8531 assert(PyDict_GET_SIZE(c->u->u_varnames) < INT_MAX);
8532 assert(PyDict_GET_SIZE(c->u->u_cellvars) < INT_MAX);
8533 assert(PyDict_GET_SIZE(c->u->u_freevars) < INT_MAX);
8534 448800 int nlocals = (int)PyDict_GET_SIZE(c->u->u_varnames);
8535 448800 int ncellvars = (int)PyDict_GET_SIZE(c->u->u_cellvars);
8536 448800 int nfreevars = (int)PyDict_GET_SIZE(c->u->u_freevars);
8537 assert(INT_MAX - nlocals - ncellvars > 0);
8538 assert(INT_MAX - nlocals - ncellvars - nfreevars > 0);
8539 448800 int nlocalsplus = nlocals + ncellvars + nfreevars;
8540 448800 int *cellfixedoffsets = build_cellfixedoffsets(c);
8541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (cellfixedoffsets == NULL) {
8542 goto error;
8543 }
8544
8545 448800 int nblocks = 0;
8546 448800 basicblock *entryblock = NULL;
8547
2/2
✓ Branch 0 taken 1366225 times.
✓ Branch 1 taken 448800 times.
1815025 for (basicblock *b = c->u->u_blocks; b != NULL; b = b->b_list) {
8548 1366225 nblocks++;
8549 1366225 entryblock = b;
8550 }
8551 assert(entryblock != NULL);
8552
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
8553 PyErr_NoMemory();
8554 goto error;
8555 }
8556
8557 /* Set firstlineno if it wasn't explicitly set. */
8558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (!c->u->u_firstlineno) {
8559 if (entryblock->b_instr && entryblock->b_instr->i_loc.lineno) {
8560 c->u->u_firstlineno = entryblock->b_instr->i_loc.lineno;
8561 }
8562 else {
8563 c->u->u_firstlineno = 1;
8564 }
8565 }
8566
8567 // This must be called before fix_cell_offsets().
8568
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (insert_prefix_instructions(c, entryblock, cellfixedoffsets, nfreevars, code_flags)) {
8569 goto error;
8570 }
8571
8572 448800 int numdropped = fix_cell_offsets(c, entryblock, cellfixedoffsets);
8573 448800 PyMem_Free(cellfixedoffsets); // At this point we're done with it.
8574 448800 cellfixedoffsets = NULL;
8575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (numdropped < 0) {
8576 goto error;
8577 }
8578 448800 nlocalsplus -= numdropped;
8579
8580 448800 consts = consts_dict_keys_inorder(c->u->u_consts);
8581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (consts == NULL) {
8582 goto error;
8583 }
8584
8585
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (optimize_cfg(entryblock, consts, c->c_const_cache)) {
8586 goto error;
8587 }
8588
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (duplicate_exits_without_lineno(entryblock)) {
8589 return NULL;
8590 }
8591
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (trim_unused_consts(entryblock, consts)) {
8592 goto error;
8593 }
8594 448800 propagate_line_numbers(entryblock);
8595 448800 guarantee_lineno_for_exits(entryblock, c->u->u_firstlineno);
8596
8597 448800 int maxdepth = stackdepth(entryblock, code_flags);
8598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (maxdepth < 0) {
8599 goto error;
8600 }
8601
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (maxdepth > MAX_ALLOWED_STACK_USE) {
8602 PyErr_Format(PyExc_SystemError,
8603 "excessive stack use: stack is %d deep",
8604 maxdepth);
8605 goto error;
8606 }
8607
8608
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (label_exception_targets(entryblock)) {
8609 goto error;
8610 }
8611 448800 convert_exception_handlers_to_nops(entryblock);
8612
8613
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (push_cold_blocks_to_end(entryblock, code_flags) < 0) {
8614 goto error;
8615 }
8616
8617 448800 remove_redundant_jumps(entryblock);
8618
2/2
✓ Branch 0 taken 1262806 times.
✓ Branch 1 taken 448800 times.
1711606 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
8619 1262806 clean_basic_block(b);
8620 }
8621
8622 /* Order of basic blocks must have been determined by now */
8623 448800 normalize_jumps(entryblock);
8624
8625
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (add_checks_for_loads_of_unknown_variables(entryblock, c) < 0) {
8626 goto error;
8627 }
8628
8629 /* Can't modify the bytecode after computing jump offsets. */
8630 448800 assemble_jump_offsets(entryblock);
8631
8632
8633 /* Create assembler */
8634 struct assembler a;
8635
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (!assemble_init(&a, c->u->u_firstlineno))
8636 goto error;
8637
8638 /* Emit code. */
8639
2/2
✓ Branch 0 taken 1262806 times.
✓ Branch 1 taken 448800 times.
1711606 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
8640
2/2
✓ Branch 0 taken 14497150 times.
✓ Branch 1 taken 1262806 times.
15759956 for (int j = 0; j < b->b_iused; j++)
8641
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14497150 times.
14497150 if (!assemble_emit(&a, &b->b_instr[j]))
8642 goto error;
8643 }
8644
8645 /* Emit location info */
8646 448800 a.a_lineno = c->u->u_firstlineno;
8647
2/2
✓ Branch 0 taken 1262806 times.
✓ Branch 1 taken 448800 times.
1711606 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
8648
2/2
✓ Branch 0 taken 14497150 times.
✓ Branch 1 taken 1262806 times.
15759956 for (int j = 0; j < b->b_iused; j++)
8649
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14497150 times.
14497150 if (!assemble_emit_location(&a, &b->b_instr[j]))
8650 goto error;
8651 }
8652
8653
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (!assemble_exception_table(&a, entryblock)) {
8654 goto error;
8655 }
8656
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (_PyBytes_Resize(&a.a_except_table, a.a_except_table_off) < 0) {
8657 goto error;
8658 }
8659
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (!merge_const_one(c->c_const_cache, &a.a_except_table)) {
8660 goto error;
8661 }
8662
8663
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (_PyBytes_Resize(&a.a_linetable, a.a_location_off) < 0) {
8664 goto error;
8665 }
8666
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (!merge_const_one(c->c_const_cache, &a.a_linetable)) {
8667 goto error;
8668 }
8669
8670
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) {
8671 goto error;
8672 }
8673
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (!merge_const_one(c->c_const_cache, &a.a_bytecode)) {
8674 goto error;
8675 }
8676
8677 448800 co = makecode(c, &a, consts, maxdepth, nlocalsplus, code_flags);
8678 448800 error:
8679 448800 Py_XDECREF(consts);
8680 448800 assemble_free(&a);
8681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (cellfixedoffsets != NULL) {
8682 PyMem_Free(cellfixedoffsets);
8683 }
8684 448800 return co;
8685 }
8686
8687 static PyObject*
8688 69151 get_const_value(int opcode, int oparg, PyObject *co_consts)
8689 {
8690 69151 PyObject *constant = NULL;
8691 assert(HAS_CONST(opcode));
8692
1/2
✓ Branch 0 taken 69151 times.
✗ Branch 1 not taken.
69151 if (opcode == LOAD_CONST) {
8693 69151 constant = PyList_GET_ITEM(co_consts, oparg);
8694 }
8695
8696
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69151 times.
69151 if (constant == NULL) {
8697 PyErr_SetString(PyExc_SystemError,
8698 "Internal error: failed to get value of a constant");
8699 return NULL;
8700 }
8701 69151 Py_INCREF(constant);
8702 69151 return constant;
8703 }
8704
8705 /* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
8706 with LOAD_CONST (c1, c2, ... cn).
8707 The consts table must still be in list form so that the
8708 new constant (c1, c2, ... cn) can be appended.
8709 Called with codestr pointing to the first LOAD_CONST.
8710 */
8711 static int
8712 218588 fold_tuple_on_constants(PyObject *const_cache,
8713 struct instr *inst,
8714 int n, PyObject *consts)
8715 {
8716 /* Pre-conditions */
8717 assert(PyDict_CheckExact(const_cache));
8718 assert(PyList_CheckExact(consts));
8719 assert(inst[n].i_opcode == BUILD_TUPLE);
8720 assert(inst[n].i_oparg == n);
8721
8722
2/2
✓ Branch 0 taken 261737 times.
✓ Branch 1 taken 19365 times.
281102 for (int i = 0; i < n; i++) {
8723
4/4
✓ Branch 0 taken 199418 times.
✓ Branch 1 taken 62319 times.
✓ Branch 2 taken 199223 times.
✓ Branch 3 taken 195 times.
261737 if (!HAS_CONST(inst[i].i_opcode)) {
8724 199223 return 0;
8725 }
8726 }
8727
8728 /* Buildup new tuple of constants */
8729 19365 PyObject *newconst = PyTuple_New(n);
8730
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19365 times.
19365 if (newconst == NULL) {
8731 return -1;
8732 }
8733
2/2
✓ Branch 0 taken 40759 times.
✓ Branch 1 taken 19365 times.
60124 for (int i = 0; i < n; i++) {
8734 40759 int op = inst[i].i_opcode;
8735 40759 int arg = inst[i].i_oparg;
8736 40759 PyObject *constant = get_const_value(op, arg, consts);
8737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40759 times.
40759 if (constant == NULL) {
8738 return -1;
8739 }
8740 40759 PyTuple_SET_ITEM(newconst, i, constant);
8741 }
8742
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19365 times.
19365 if (merge_const_one(const_cache, &newconst) == 0) {
8743 Py_DECREF(newconst);
8744 return -1;
8745 }
8746
8747 Py_ssize_t index;
8748
2/2
✓ Branch 1 taken 634730 times.
✓ Branch 2 taken 15563 times.
650293 for (index = 0; index < PyList_GET_SIZE(consts); index++) {
8749
2/2
✓ Branch 0 taken 3802 times.
✓ Branch 1 taken 630928 times.
634730 if (PyList_GET_ITEM(consts, index) == newconst) {
8750 3802 break;
8751 }
8752 }
8753
2/2
✓ Branch 1 taken 15563 times.
✓ Branch 2 taken 3802 times.
19365 if (index == PyList_GET_SIZE(consts)) {
8754
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15563 times.
15563 if ((size_t)index >= (size_t)INT_MAX - 1) {
8755 Py_DECREF(newconst);
8756 PyErr_SetString(PyExc_OverflowError, "too many constants");
8757 return -1;
8758 }
8759
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15563 times.
15563 if (PyList_Append(consts, newconst)) {
8760 Py_DECREF(newconst);
8761 return -1;
8762 }
8763 }
8764 19365 Py_DECREF(newconst);
8765
2/2
✓ Branch 0 taken 40759 times.
✓ Branch 1 taken 19365 times.
60124 for (int i = 0; i < n; i++) {
8766 40759 inst[i].i_opcode = NOP;
8767 }
8768 19365 inst[n].i_opcode = LOAD_CONST;
8769 19365 inst[n].i_oparg = (int)index;
8770 19365 return 0;
8771 }
8772
8773 #define VISITED (-1)
8774
8775 // Replace an arbitrary run of SWAPs and NOPs with an optimal one that has the
8776 // same effect.
8777 static int
8778 13163 swaptimize(basicblock *block, int *ix)
8779 {
8780 // NOTE: "./python -m test test_patma" serves as a good, quick stress test
8781 // for this function. Make sure to blow away cached *.pyc files first!
8782 assert(*ix < block->b_iused);
8783 13163 struct instr *instructions = &block->b_instr[*ix];
8784 // Find the length of the current sequence of SWAPs and NOPs, and record the
8785 // maximum depth of the stack manipulations:
8786 assert(instructions[0].i_opcode == SWAP);
8787 13163 int depth = instructions[0].i_oparg;
8788 13163 int len = 0;
8789 13163 int more = false;
8790 13163 int limit = block->b_iused - *ix;
8791
1/2
✓ Branch 0 taken 15094 times.
✗ Branch 1 not taken.
15094 while (++len < limit) {
8792 15094 int opcode = instructions[len].i_opcode;
8793
2/2
✓ Branch 0 taken 1931 times.
✓ Branch 1 taken 13163 times.
15094 if (opcode == SWAP) {
8794 1931 depth = Py_MAX(depth, instructions[len].i_oparg);
8795 1931 more = true;
8796 }
8797
1/2
✓ Branch 0 taken 13163 times.
✗ Branch 1 not taken.
13163 else if (opcode != NOP) {
8798 13163 break;
8799 }
8800 }
8801 // It's already optimal if there's only one SWAP:
8802
2/2
✓ Branch 0 taken 11232 times.
✓ Branch 1 taken 1931 times.
13163 if (!more) {
8803 11232 return 0;
8804 }
8805 // Create an array with elements {0, 1, 2, ..., depth - 1}:
8806 1931 int *stack = PyMem_Malloc(depth * sizeof(int));
8807
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1931 times.
1931 if (stack == NULL) {
8808 PyErr_NoMemory();
8809 return -1;
8810 }
8811
2/2
✓ Branch 0 taken 5793 times.
✓ Branch 1 taken 1931 times.
7724 for (int i = 0; i < depth; i++) {
8812 5793 stack[i] = i;
8813 }
8814 // Simulate the combined effect of these instructions by "running" them on
8815 // our "stack":
8816
2/2
✓ Branch 0 taken 3862 times.
✓ Branch 1 taken 1931 times.
5793 for (int i = 0; i < len; i++) {
8817
1/2
✓ Branch 0 taken 3862 times.
✗ Branch 1 not taken.
3862 if (instructions[i].i_opcode == SWAP) {
8818 3862 int oparg = instructions[i].i_oparg;
8819 3862 int top = stack[0];
8820 // SWAPs are 1-indexed:
8821 3862 stack[0] = stack[oparg - 1];
8822 3862 stack[oparg - 1] = top;
8823 }
8824 }
8825 // Now we can begin! Our approach here is based on a solution to a closely
8826 // related problem (https://cs.stackexchange.com/a/13938). It's easiest to
8827 // think of this algorithm as determining the steps needed to efficiently
8828 // "un-shuffle" our stack. By performing the moves in *reverse* order,
8829 // though, we can efficiently *shuffle* it! For this reason, we will be
8830 // replacing instructions starting from the *end* of the run. Since the
8831 // solution is optimal, we don't need to worry about running out of space:
8832 1931 int current = len - 1;
8833
2/2
✓ Branch 0 taken 5793 times.
✓ Branch 1 taken 1931 times.
7724 for (int i = 0; i < depth; i++) {
8834 // Skip items that have already been visited, or just happen to be in
8835 // the correct location:
8836
3/4
✓ Branch 0 taken 1931 times.
✓ Branch 1 taken 3862 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1931 times.
5793 if (stack[i] == VISITED || stack[i] == i) {
8837 3862 continue;
8838 }
8839 // Okay, we've found an item that hasn't been visited. It forms a cycle
8840 // with other items; traversing the cycle and swapping each item with
8841 // the next will put them all in the correct place. The weird
8842 // loop-and-a-half is necessary to insert 0 into every cycle, since we
8843 // can only swap from that position:
8844 1931 int j = i;
8845 5793 while (true) {
8846 // Skip the actual swap if our item is zero, since swapping the top
8847 // item with itself is pointless:
8848
2/2
✓ Branch 0 taken 3862 times.
✓ Branch 1 taken 3862 times.
7724 if (j) {
8849 assert(0 <= current);
8850 // SWAPs are 1-indexed:
8851 3862 instructions[current].i_opcode = SWAP;
8852 3862 instructions[current--].i_oparg = j + 1;
8853 }
8854
2/2
✓ Branch 0 taken 1931 times.
✓ Branch 1 taken 5793 times.
7724 if (stack[j] == VISITED) {
8855 // Completed the cycle:
8856 assert(j == i);
8857 1931 break;
8858 }
8859 5793 int next_j = stack[j];
8860 5793 stack[j] = VISITED;
8861 5793 j = next_j;
8862 }
8863 }
8864 // NOP out any unused instructions:
8865
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1931 times.
1931 while (0 <= current) {
8866 instructions[current--].i_opcode = NOP;
8867 }
8868 1931 PyMem_Free(stack);
8869 1931 *ix += len - 1;
8870 1931 return 0;
8871 }
8872
8873 // This list is pretty small, since it's only okay to reorder opcodes that:
8874 // - can't affect control flow (like jumping or raising exceptions)
8875 // - can't invoke arbitrary code (besides finalizers)
8876 // - only touch the TOS (and pop it when finished)
8877 #define SWAPPABLE(opcode) \
8878 ((opcode) == STORE_FAST || (opcode) == POP_TOP)
8879
8880 static int
8881 17732 next_swappable_instruction(basicblock *block, int i, int lineno)
8882 {
8883
2/2
✓ Branch 0 taken 17660 times.
✓ Branch 1 taken 72 times.
17732 while (++i < block->b_iused) {
8884 17660 struct instr *instruction = &block->b_instr[i];
8885
4/4
✓ Branch 0 taken 4492 times.
✓ Branch 1 taken 13168 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 4476 times.
17660 if (0 <= lineno && instruction->i_loc.lineno != lineno) {
8886 // Optimizing across this instruction could cause user-visible
8887 // changes in the names bound between line tracing events!
8888 16 return -1;
8889 }
8890
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17644 times.
17644 if (instruction->i_opcode == NOP) {
8891 continue;
8892 }
8893
4/4
✓ Branch 0 taken 12091 times.
✓ Branch 1 taken 5553 times.
✓ Branch 2 taken 1568 times.
✓ Branch 3 taken 10523 times.
17644 if (SWAPPABLE(instruction->i_opcode)) {
8894 7121 return i;
8895 }
8896 10523 return -1;
8897 }
8898 72 return -1;
8899 }
8900
8901 // Attempt to apply SWAPs statically by swapping *instructions* rather than
8902 // stack items. For example, we can replace SWAP(2), POP_TOP, STORE_FAST(42)
8903 // with the more efficient NOP, STORE_FAST(42), POP_TOP.
8904 static void
8905 13163 apply_static_swaps(basicblock *block, int i)
8906 {
8907 // SWAPs are to our left, and potential swaperands are to our right:
8908
2/2
✓ Branch 0 taken 18264 times.
✓ Branch 1 taken 3 times.
18267 for (; 0 <= i; i--) {
8909 assert(i < block->b_iused);
8910 18264 struct instr *swap = &block->b_instr[i];
8911
2/2
✓ Branch 0 taken 5101 times.
✓ Branch 1 taken 13163 times.
18264 if (swap->i_opcode != SWAP) {
8912
4/6
✓ Branch 0 taken 2549 times.
✓ Branch 1 taken 2552 times.
✓ Branch 2 taken 2549 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2549 times.
5101 if (swap->i_opcode == NOP || SWAPPABLE(swap->i_opcode)) {
8913 // Nope, but we know how to handle these. Keep looking:
8914 2552 continue;
8915 }
8916 // We can't reason about what this instruction does. Bail:
8917 13160 return;
8918 }
8919 13163 int j = next_swappable_instruction(block, i, -1);
8920
2/2
✓ Branch 0 taken 8976 times.
✓ Branch 1 taken 4187 times.
13163 if (j < 0) {
8921 8976 return;
8922 }
8923 4187 int k = j;
8924 4187 int lineno = block->b_instr[j].i_loc.lineno;
8925
2/2
✓ Branch 0 taken 4569 times.
✓ Branch 1 taken 2552 times.
7121 for (int count = swap->i_oparg - 1; 0 < count; count--) {
8926 4569 k = next_swappable_instruction(block, k, lineno);
8927
2/2
✓ Branch 0 taken 1635 times.
✓ Branch 1 taken 2934 times.
4569 if (k < 0) {
8928 1635 return;
8929 }
8930 }
8931 // Success!
8932 2552 swap->i_opcode = NOP;
8933 2552 struct instr temp = block->b_instr[j];
8934 2552 block->b_instr[j] = block->b_instr[k];
8935 2552 block->b_instr[k] = temp;
8936 }
8937 }
8938
8939 // Attempt to eliminate jumps to jumps by updating inst to jump to
8940 // target->i_target using the provided opcode. Return whether or not the
8941 // optimization was successful.
8942 static bool
8943 26008 jump_thread(struct instr *inst, struct instr *target, int opcode)
8944 {
8945 assert(!IS_VIRTUAL_OPCODE(opcode) || IS_VIRTUAL_JUMP_OPCODE(opcode));
8946 assert(is_jump(inst));
8947 assert(is_jump(target));
8948 // bpo-45773: If inst->i_target == target->i_target, then nothing actually
8949 // changes (and we fall into an infinite loop):
8950
4/4
✓ Branch 0 taken 14057 times.
✓ Branch 1 taken 11951 times.
✓ Branch 2 taken 14045 times.
✓ Branch 3 taken 12 times.
26008 if ((inst->i_loc.lineno == target->i_loc.lineno || target->i_loc.lineno == -1) &&
8951
1/2
✓ Branch 0 taken 25996 times.
✗ Branch 1 not taken.
25996 inst->i_target != target->i_target)
8952 {
8953 25996 inst->i_target = target->i_target;
8954 25996 inst->i_opcode = opcode;
8955 25996 return true;
8956 }
8957 12 return false;
8958 }
8959
8960 /* Maximum size of basic block that should be copied in optimizer */
8961 #define MAX_COPY_SIZE 4
8962
8963 /* Optimization */
8964 static int
8965 1366225 optimize_basic_block(PyObject *const_cache, basicblock *bb, PyObject *consts)
8966 {
8967 assert(PyDict_CheckExact(const_cache));
8968 assert(PyList_CheckExact(consts));
8969 struct instr nop;
8970 1366225 nop.i_opcode = NOP;
8971 struct instr *target;
8972
2/2
✓ Branch 0 taken 15816526 times.
✓ Branch 1 taken 1366225 times.
17182751 for (int i = 0; i < bb->b_iused; i++) {
8973 15816526 struct instr *inst = &bb->b_instr[i];
8974 15816526 int oparg = inst->i_oparg;
8975
2/2
✓ Branch 0 taken 14496511 times.
✓ Branch 1 taken 1320015 times.
15816526 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
8976
4/4
✓ Branch 1 taken 15339105 times.
✓ Branch 2 taken 477421 times.
✓ Branch 4 taken 37487 times.
✓ Branch 5 taken 15301618 times.
15816526 if (is_jump(inst) || is_block_push(inst)) {
8977 /* Skip over empty basic blocks. */
8978
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 514908 times.
514908 while (inst->i_target->b_iused == 0) {
8979 inst->i_target = inst->i_target->b_next;
8980 }
8981 514908 target = &inst->i_target->b_instr[0];
8982 514908 assert(!IS_ASSEMBLER_OPCODE(target->i_opcode));
8983 }
8984 else {
8985 15301618 target = &nop;
8986 }
8987 assert(!IS_ASSEMBLER_OPCODE(inst->i_opcode));
8988
13/13
✓ Branch 0 taken 2251622 times.
✓ Branch 1 taken 222138 times.
✓ Branch 2 taken 8966 times.
✓ Branch 3 taken 4811 times.
✓ Branch 4 taken 21224 times.
✓ Branch 5 taken 157245 times.
✓ Branch 6 taken 107945 times.
✓ Branch 7 taken 123506 times.
✓ Branch 8 taken 50394 times.
✓ Branch 9 taken 13163 times.
✓ Branch 10 taken 53723 times.
✓ Branch 11 taken 1134912 times.
✓ Branch 12 taken 11666877 times.
15816526 switch (inst->i_opcode) {
8989 /* Remove LOAD_CONST const; conditional jump */
8990
4/4
✓ Branch 0 taken 2188 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 26200 times.
✓ Branch 3 taken 2223230 times.
2251622 case LOAD_CONST:
8991 {
8992 PyObject* cnt;
8993 int is_true;
8994 int jump_if_true;
8995 switch(nextop) {
8996 2188 case POP_JUMP_IF_FALSE:
8997 case POP_JUMP_IF_TRUE:
8998 2188 cnt = get_const_value(inst->i_opcode, oparg, consts);
8999
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2188 times.
2188 if (cnt == NULL) {
9000 goto error;
9001 }
9002 2188 is_true = PyObject_IsTrue(cnt);
9003 2188 Py_DECREF(cnt);
9004
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2188 times.
2188 if (is_true == -1) {
9005 goto error;
9006 }
9007 2188 inst->i_opcode = NOP;
9008 2188 jump_if_true = nextop == POP_JUMP_IF_TRUE;
9009
2/2
✓ Branch 0 taken 1048 times.
✓ Branch 1 taken 1140 times.
2188 if (is_true == jump_if_true) {
9010 1048 bb->b_instr[i+1].i_opcode = JUMP;
9011 }
9012 else {
9013 1140 bb->b_instr[i+1].i_opcode = NOP;
9014 }
9015 2188 break;
9016 4 case JUMP_IF_FALSE_OR_POP:
9017 case JUMP_IF_TRUE_OR_POP:
9018 4 cnt = get_const_value(inst->i_opcode, oparg, consts);
9019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (cnt == NULL) {
9020 goto error;
9021 }
9022 4 is_true = PyObject_IsTrue(cnt);
9023 4 Py_DECREF(cnt);
9024
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (is_true == -1) {
9025 goto error;
9026 }
9027 4 jump_if_true = nextop == JUMP_IF_TRUE_OR_POP;
9028
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (is_true == jump_if_true) {
9029 bb->b_instr[i+1].i_opcode = JUMP;
9030 }
9031 else {
9032 4 inst->i_opcode = NOP;
9033 4 bb->b_instr[i+1].i_opcode = NOP;
9034 }
9035 4 break;
9036 26200 case IS_OP:
9037 26200 cnt = get_const_value(inst->i_opcode, oparg, consts);
9038
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26200 times.
26200 if (cnt == NULL) {
9039 goto error;
9040 }
9041
2/2
✓ Branch 0 taken 26091 times.
✓ Branch 1 taken 109 times.
26200 int jump_op = i+2 < bb->b_iused ? bb->b_instr[i+2].i_opcode : 0;
9042
6/6
✓ Branch 0 taken 20990 times.
✓ Branch 1 taken 5210 times.
✓ Branch 2 taken 3733 times.
✓ Branch 3 taken 17257 times.
✓ Branch 4 taken 2963 times.
✓ Branch 5 taken 770 times.
26200 if (Py_IsNone(cnt) && (jump_op == POP_JUMP_IF_FALSE || jump_op == POP_JUMP_IF_TRUE)) {
9043 20220 unsigned char nextarg = bb->b_instr[i+1].i_oparg;
9044 20220 inst->i_opcode = NOP;
9045 20220 bb->b_instr[i+1].i_opcode = NOP;
9046 20220 bb->b_instr[i+2].i_opcode = nextarg ^ (jump_op == POP_JUMP_IF_FALSE) ?
9047
2/2
✓ Branch 0 taken 8595 times.
✓ Branch 1 taken 11625 times.
20220 POP_JUMP_IF_NOT_NONE : POP_JUMP_IF_NONE;
9048 }
9049 26200 Py_DECREF(cnt);
9050 26200 break;
9051 }
9052 2251622 break;
9053 }
9054
9055 /* Try to fold tuples of constants.
9056 Skip over BUILD_TUPLE(1) UNPACK_SEQUENCE(1).
9057 Replace BUILD_TUPLE(2) UNPACK_SEQUENCE(2) with SWAP(2).
9058 Replace BUILD_TUPLE(3) UNPACK_SEQUENCE(3) with SWAP(3). */
9059 222138 case BUILD_TUPLE:
9060
3/4
✓ Branch 0 taken 3242 times.
✓ Branch 1 taken 218896 times.
✓ Branch 2 taken 3242 times.
✗ Branch 3 not taken.
222138 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
9061 3141 switch(oparg) {
9062 case 1:
9063 inst->i_opcode = NOP;
9064 bb->b_instr[i+1].i_opcode = NOP;
9065 continue;
9066 3141 case 2:
9067 case 3:
9068 3141 inst->i_opcode = NOP;
9069 3141 bb->b_instr[i+1].i_opcode = SWAP;
9070 3141 continue;
9071 }
9072 }
9073
2/2
✓ Branch 0 taken 218588 times.
✓ Branch 1 taken 409 times.
218997 if (i >= oparg) {
9074
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 218588 times.
218588 if (fold_tuple_on_constants(const_cache, inst-oparg, oparg, consts)) {
9075 goto error;
9076 }
9077 }
9078 218997 break;
9079
9080 /* Simplify conditional jump to conditional jump where the
9081 result of the first test implies the success of a similar
9082 test or the failure of the opposite test.
9083 Arises in code like:
9084 "a and b or c"
9085 "(a and b) and c"
9086 "(a or b) or c"
9087 "(a or b) and c"
9088 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
9089 --> x:JUMP_IF_FALSE_OR_POP z
9090 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
9091 --> x:POP_JUMP_IF_FALSE y+1
9092 where y+1 is the instruction following the second test.
9093 */
9094 8966 case JUMP_IF_FALSE_OR_POP:
9095
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 326 times.
✓ Branch 3 taken 8630 times.
8966 switch (target->i_opcode) {
9096 case POP_JUMP_IF_FALSE:
9097 i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
9098 break;
9099 10 case JUMP:
9100 case JUMP_IF_FALSE_OR_POP:
9101 10 i -= jump_thread(inst, target, JUMP_IF_FALSE_OR_POP);
9102 10 break;
9103 326 case JUMP_IF_TRUE_OR_POP:
9104 case POP_JUMP_IF_TRUE:
9105
2/2
✓ Branch 0 taken 309 times.
✓ Branch 1 taken 17 times.
326 if (inst->i_loc.lineno == target->i_loc.lineno) {
9106 // We don't need to bother checking for loops here,
9107 // since a block's b_next cannot point to itself:
9108 assert(inst->i_target != inst->i_target->b_next);
9109 309 inst->i_opcode = POP_JUMP_IF_FALSE;
9110 309 inst->i_target = inst->i_target->b_next;
9111 309 --i;
9112 }
9113 326 break;
9114 }
9115 8966 break;
9116 4811 case JUMP_IF_TRUE_OR_POP:
9117
3/4
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 39 times.
✓ Branch 3 taken 4757 times.
4811 switch (target->i_opcode) {
9118 case POP_JUMP_IF_TRUE:
9119 i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
9120 break;
9121 15 case JUMP:
9122 case JUMP_IF_TRUE_OR_POP:
9123 15 i -= jump_thread(inst, target, JUMP_IF_TRUE_OR_POP);
9124 15 break;
9125 39 case JUMP_IF_FALSE_OR_POP:
9126 case POP_JUMP_IF_FALSE:
9127
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 21 times.
39 if (inst->i_loc.lineno == target->i_loc.lineno) {
9128 // We don't need to bother checking for loops here,
9129 // since a block's b_next cannot point to itself:
9130 assert(inst->i_target != inst->i_target->b_next);
9131 18 inst->i_opcode = POP_JUMP_IF_TRUE;
9132 18 inst->i_target = inst->i_target->b_next;
9133 18 --i;
9134 }
9135 39 break;
9136 }
9137 4811 break;
9138 21224 case POP_JUMP_IF_NOT_NONE:
9139 case POP_JUMP_IF_NONE:
9140
2/2
✓ Branch 0 taken 1004 times.
✓ Branch 1 taken 20220 times.
21224 switch (target->i_opcode) {
9141 1004 case JUMP:
9142 1004 i -= jump_thread(inst, target, inst->i_opcode);
9143 }
9144 21224 break;
9145 157245 case POP_JUMP_IF_FALSE:
9146
2/2
✓ Branch 0 taken 12236 times.
✓ Branch 1 taken 145009 times.
157245 switch (target->i_opcode) {
9147 12236 case JUMP:
9148 12236 i -= jump_thread(inst, target, POP_JUMP_IF_FALSE);
9149 }
9150 157245 break;
9151 107945 case POP_JUMP_IF_TRUE:
9152
2/2
✓ Branch 0 taken 2917 times.
✓ Branch 1 taken 105028 times.
107945 switch (target->i_opcode) {
9153 2917 case JUMP:
9154 2917 i -= jump_thread(inst, target, POP_JUMP_IF_TRUE);
9155 }
9156 107945 break;
9157 123506 case JUMP:
9158
2/2
✓ Branch 0 taken 9826 times.
✓ Branch 1 taken 113680 times.
123506 switch (target->i_opcode) {
9159 9826 case JUMP:
9160 9826 i -= jump_thread(inst, target, JUMP);
9161 }
9162 123506 break;
9163 50394 case FOR_ITER:
9164 50394 if (target->i_opcode == JUMP) {
9165 /* This will not work now because the jump (at target) could
9166 * be forward or backward and FOR_ITER only jumps forward. We
9167 * can re-enable this if ever we implement a backward version
9168 * of FOR_ITER.
9169 */
9170 /*
9171 i -= jump_thread(inst, target, FOR_ITER);
9172 */
9173 }
9174 50394 break;
9175 13163 case SWAP:
9176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13163 times.
13163 if (oparg == 1) {
9177 inst->i_opcode = NOP;
9178 break;
9179 }
9180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 13163 times.
13163 if (swaptimize(bb, &i)) {
9181 goto error;
9182 }
9183 13163 apply_static_swaps(bb, i);
9184 13163 break;
9185 53723 case KW_NAMES:
9186 53723 break;
9187 1134912 case PUSH_NULL:
9188
3/4
✓ Branch 0 taken 1024429 times.
✓ Branch 1 taken 110483 times.
✓ Branch 2 taken 1024429 times.
✗ Branch 3 not taken.
1134912 if (nextop == LOAD_GLOBAL && (inst[1].i_opcode & 1) == 0) {
9189 1024429 inst->i_opcode = NOP;
9190 1024429 inst->i_oparg = 0;
9191 1024429 inst[1].i_oparg |= 1;
9192 }
9193 1134912 break;
9194 15816526 default:
9195 /* All HAS_CONST opcodes should be handled with LOAD_CONST */
9196 assert (!HAS_CONST(inst->i_opcode));
9197 }
9198 }
9199 1366225 return 0;
9200 error:
9201 return -1;
9202 }
9203
9204 /* If this block ends with an unconditional jump to an exit block,
9205 * then remove the jump and extend this block with the target.
9206 */
9207 static int
9208 2732450 extend_block(basicblock *bb) {
9209
2/2
✓ Branch 0 taken 145154 times.
✓ Branch 1 taken 2587296 times.
2732450 if (bb->b_iused == 0) {
9210 145154 return 0;
9211 }
9212 2587296 struct instr *last = &bb->b_instr[bb->b_iused-1];
9213
2/2
✓ Branch 0 taken 2332711 times.
✓ Branch 1 taken 254585 times.
2587296 if (last->i_opcode != JUMP &&
9214
1/2
✓ Branch 0 taken 2332711 times.
✗ Branch 1 not taken.
2332711 last->i_opcode != JUMP_FORWARD &&
9215
1/2
✓ Branch 0 taken 2332711 times.
✗ Branch 1 not taken.
2332711 last->i_opcode != JUMP_BACKWARD) {
9216 2332711 return 0;
9217 }
9218
4/4
✓ Branch 1 taken 52255 times.
✓ Branch 2 taken 202330 times.
✓ Branch 3 taken 30597 times.
✓ Branch 4 taken 21658 times.
254585 if (basicblock_exits_scope(last->i_target) && last->i_target->b_iused <= MAX_COPY_SIZE) {
9219 30597 basicblock *to_copy = last->i_target;
9220 30597 last->i_opcode = NOP;
9221
2/2
✓ Branch 0 taken 66957 times.
✓ Branch 1 taken 30597 times.
97554 for (int i = 0; i < to_copy->b_iused; i++) {
9222 66957 int index = basicblock_next_instr(bb);
9223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66957 times.
66957 if (index < 0) {
9224 return -1;
9225 }
9226 66957 bb->b_instr[index] = to_copy->b_instr[i];
9227 }
9228 }
9229 254585 return 0;
9230 }
9231
9232 static void
9233 3891718 clean_basic_block(basicblock *bb) {
9234 /* Remove NOPs when legal to do so. */
9235 3891718 int dest = 0;
9236 3891718 int prev_lineno = -1;
9237
2/2
✓ Branch 0 taken 44883453 times.
✓ Branch 1 taken 3891718 times.
48775171 for (int src = 0; src < bb->b_iused; src++) {
9238 44883453 int lineno = bb->b_instr[src].i_loc.lineno;
9239
2/2
✓ Branch 0 taken 1271777 times.
✓ Branch 1 taken 43611676 times.
44883453 if (bb->b_instr[src].i_opcode == NOP) {
9240 /* Eliminate no-op if it doesn't have a line number */
9241
2/2
✓ Branch 0 taken 42319 times.
✓ Branch 1 taken 1229458 times.
1271777 if (lineno < 0) {
9242 42319 continue;
9243 }
9244 /* or, if the previous instruction had the same line number. */
9245
2/2
✓ Branch 0 taken 784494 times.
✓ Branch 1 taken 444964 times.
1229458 if (prev_lineno == lineno) {
9246 784494 continue;
9247 }
9248 /* or, if the next instruction has same line number or no line number */
9249
2/2
✓ Branch 0 taken 427731 times.
✓ Branch 1 taken 17233 times.
444964 if (src < bb->b_iused - 1) {
9250 427731 int next_lineno = bb->b_instr[src+1].i_loc.lineno;
9251
4/4
✓ Branch 0 taken 422278 times.
✓ Branch 1 taken 5453 times.
✓ Branch 2 taken 399536 times.
✓ Branch 3 taken 22742 times.
427731 if (next_lineno < 0 || next_lineno == lineno) {
9252 404989 bb->b_instr[src+1].i_loc = bb->b_instr[src].i_loc;
9253 404989 continue;
9254 }
9255 }
9256 else {
9257 17233 basicblock* next = bb->b_next;
9258
3/4
✓ Branch 0 taken 17371 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✓ Branch 3 taken 17233 times.
17371 while (next && next->b_iused == 0) {
9259 138 next = next->b_next;
9260 }
9261 /* or if last instruction in BB and next BB has same line number */
9262
1/2
✓ Branch 0 taken 17233 times.
✗ Branch 1 not taken.
17233 if (next) {
9263
2/2
✓ Branch 0 taken 305 times.
✓ Branch 1 taken 16928 times.
17233 if (lineno == next->b_instr[0].i_loc.lineno) {
9264 305 continue;
9265 }
9266 }
9267 }
9268
9269 }
9270
2/2
✓ Branch 0 taken 7954085 times.
✓ Branch 1 taken 35697261 times.
43651346 if (dest != src) {
9271 7954085 bb->b_instr[dest] = bb->b_instr[src];
9272 }
9273 43651346 dest++;
9274 43651346 prev_lineno = lineno;
9275 }
9276 assert(dest <= bb->b_iused);
9277 3891718 bb->b_iused = dest;
9278 3891718 }
9279
9280 static int
9281 1366225 normalize_basic_block(basicblock *bb) {
9282 /* Mark blocks as exit and/or nofallthrough.
9283 Raise SystemError if CFG is malformed. */
9284
2/2
✓ Branch 0 taken 15673699 times.
✓ Branch 1 taken 1366225 times.
17039924 for (int i = 0; i < bb->b_iused; i++) {
9285 15673699 int opcode = bb->b_instr[i].i_opcode;
9286 assert(!IS_ASSEMBLER_OPCODE(opcode));
9287
12/14
✓ Branch 0 taken 15532803 times.
✓ Branch 1 taken 140896 times.
✓ Branch 2 taken 15531138 times.
✓ Branch 3 taken 1665 times.
✓ Branch 4 taken 15531138 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15531138 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 15368126 times.
✓ Branch 9 taken 163012 times.
✓ Branch 10 taken 15259020 times.
✓ Branch 11 taken 109106 times.
✓ Branch 13 taken 65818 times.
✓ Branch 14 taken 15193202 times.
15673699 int is_jump = IS_JUMP_OPCODE(opcode);
9288
6/6
✓ Branch 0 taken 15173385 times.
✓ Branch 1 taken 500314 times.
✓ Branch 2 taken 15074616 times.
✓ Branch 3 taken 98769 times.
✓ Branch 4 taken 37484 times.
✓ Branch 5 taken 15037132 times.
15673699 int is_exit = IS_SCOPE_EXIT_OPCODE(opcode);
9289
4/4
✓ Branch 0 taken 15037132 times.
✓ Branch 1 taken 636567 times.
✓ Branch 2 taken 480497 times.
✓ Branch 3 taken 14556635 times.
15673699 if (is_exit || is_jump) {
9290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1117064 times.
1117064 if (i != bb->b_iused-1) {
9291 PyErr_SetString(PyExc_SystemError, "malformed control flow graph.");
9292 return -1;
9293 }
9294 }
9295
2/2
✓ Branch 0 taken 480497 times.
✓ Branch 1 taken 15193202 times.
15673699 if (is_jump) {
9296 /* Skip over empty basic blocks. */
9297
2/2
✓ Branch 0 taken 96239 times.
✓ Branch 1 taken 480497 times.
576736 while (bb->b_instr[i].i_target->b_iused == 0) {
9298 96239 bb->b_instr[i].i_target = bb->b_instr[i].i_target->b_next;
9299 }
9300 }
9301 }
9302 1366225 return 0;
9303 }
9304
9305 static int
9306 448800 mark_reachable(basicblock *entryblock) {
9307 448800 basicblock **stack = make_cfg_traversal_stack(entryblock);
9308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 448800 times.
448800 if (stack == NULL) {
9309 return -1;
9310 }
9311 448800 basicblock **sp = stack;
9312 448800 entryblock->b_predecessors = 1;
9313 448800 *sp++ = entryblock;
9314
2/2
✓ Branch 0 taken 1263473 times.
✓ Branch 1 taken 448800 times.
1712273 while (sp > stack) {
9315 1263473 basicblock *b = *(--sp);
9316 1263473 b->b_visited = 1;
9317
4/4
✓ Branch 0 taken 829236 times.
✓ Branch 1 taken 434237 times.
✓ Branch 3 taken 528542 times.
✓ Branch 4 taken 300694 times.
1263473 if (b->b_next && BB_HAS_FALLTHROUGH(b)) {
9318
2/2
✓ Branch 0 taken 471838 times.
✓ Branch 1 taken 56704 times.
528542 if (!b->b_next->b_visited) {
9319 assert(b->b_next->b_predecessors == 0);
9320 471838 *sp++ = b->b_next;
9321 }
9322 528542 b->b_next->b_predecessors++;
9323 }
9324
2/2
✓ Branch 0 taken 14528576 times.
✓ Branch 1 taken 1263473 times.
15792049 for (int i = 0; i < b->b_iused; i++) {
9325 basicblock *target;
9326 14528576 struct instr *instr = &b->b_instr[i];
9327
4/4
✓ Branch 1 taken 14095545 times.
✓ Branch 2 taken 433031 times.
✓ Branch 4 taken 37475 times.
✓ Branch 5 taken 14058070 times.
14528576 if (is_jump(instr) || is_block_push(instr)) {
9328 470506 target = instr->i_target;
9329
2/2
✓ Branch 0 taken 342835 times.
✓ Branch 1 taken 127671 times.
470506 if (!target->b_visited) {
9330 assert(target->b_predecessors == 0 || target == b->b_next);
9331 342835 *sp++ = target;
9332 }
9333 470506 target->b_predecessors++;
9334
2/2
✓ Branch 1 taken 37475 times.
✓ Branch 2 taken 433031 times.
470506 if (is_block_push(instr)) {
9335 37475 target->b_except_predecessors++;
9336 }
9337 assert(target->b_except_predecessors == 0 ||
9338 target->b_except_predecessors == target->b_predecessors);
9339 }
9340 }
9341 }
9342 448800 PyMem_Free(stack);
9343 448800 return 0;
9344 }
9345
9346 static void
9347 455854 eliminate_empty_basic_blocks(basicblock *entryblock) {
9348 /* Eliminate empty blocks */
9349
2/2
✓ Branch 0 taken 1420067 times.
✓ Branch 1 taken 455854 times.
1875921 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9350 1420067 basicblock *next = b->b_next;
9351
2/2
✓ Branch 0 taken 964213 times.
✓ Branch 1 taken 455854 times.
1420067 if (next) {
9352
4/4
✓ Branch 0 taken 118101 times.
✓ Branch 1 taken 949650 times.
✓ Branch 2 taken 103538 times.
✓ Branch 3 taken 14563 times.
1067751 while (next->b_iused == 0 && next->b_next) {
9353 103538 next = next->b_next;
9354 }
9355 964213 b->b_next = next;
9356 }
9357 }
9358
2/2
✓ Branch 0 taken 1420067 times.
✓ Branch 1 taken 455854 times.
1875921 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9359
2/2
✓ Branch 0 taken 14563 times.
✓ Branch 1 taken 1405504 times.
1420067 if (b->b_iused == 0) {
9360 14563 continue;
9361 }
9362
2/2
✓ Branch 0 taken 15624254 times.
✓ Branch 1 taken 1405504 times.
17029758 for (int i = 0; i < b->b_iused; i++) {
9363 15624254 struct instr *instr = &b->b_instr[i];
9364
4/4
✓ Branch 1 taken 15125365 times.
✓ Branch 2 taken 498889 times.
✓ Branch 4 taken 37475 times.
✓ Branch 5 taken 15087890 times.
15624254 if (is_jump(instr) || is_block_push(instr)) {
9365 536364 basicblock *target = instr->i_target;
9366
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 536364 times.
536427 while (target->b_iused == 0) {
9367 63 target = target->b_next;
9368 }
9369 536364 instr->i_target = target;
9370 }
9371 }
9372 }
9373 455854 }
9374
9375
9376 /* If an instruction has no line number, but it's predecessor in the BB does,
9377 * then copy the line number. If a successor block has no line number, and only
9378 * one predecessor, then inherit the line number.
9379 * This ensures that all exit blocks (with one predecessor) receive a line number.
9380 * Also reduces the size of the line number table,
9381 * but has no impact on the generated line number events.
9382 */
9383 static void
9384 448800 propagate_line_numbers(basicblock *entryblock) {
9385
2/2
✓ Branch 0 taken 1262784 times.
✓ Branch 1 taken 448800 times.
1711584 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1262784 times.
1262784 if (b->b_iused == 0) {
9387 continue;
9388 }
9389
9390 1262784 struct location prev_location = NO_LOCATION;
9391
2/2
✓ Branch 0 taken 14562721 times.
✓ Branch 1 taken 1262784 times.
15825505 for (int i = 0; i < b->b_iused; i++) {
9392
2/2
✓ Branch 0 taken 923932 times.
✓ Branch 1 taken 13638789 times.
14562721 if (b->b_instr[i].i_loc.lineno < 0) {
9393 923932 b->b_instr[i].i_loc = prev_location;
9394 }
9395 else {
9396 13638789 prev_location = b->b_instr[i].i_loc;
9397 }
9398 }
9399
4/4
✓ Branch 1 taken 513193 times.
✓ Branch 2 taken 749591 times.
✓ Branch 3 taken 398169 times.
✓ Branch 4 taken 115024 times.
1262784 if (BB_HAS_FALLTHROUGH(b) && b->b_next->b_predecessors == 1) {
9400 assert(b->b_next->b_iused);
9401
2/2
✓ Branch 0 taken 20049 times.
✓ Branch 1 taken 378120 times.
398169 if (b->b_next->b_instr[0].i_loc.lineno < 0) {
9402 20049 b->b_next->b_instr[0].i_loc = prev_location;
9403 }
9404 }
9405
2/2
✓ Branch 1 taken 433031 times.
✓ Branch 2 taken 829753 times.
1262784 if (is_jump(&b->b_instr[b->b_iused-1])) {
9406 433031 basicblock *target = b->b_instr[b->b_iused-1].i_target;
9407
2/2
✓ Branch 0 taken 244657 times.
✓ Branch 1 taken 188374 times.
433031 if (target->b_predecessors == 1) {
9408
2/2
✓ Branch 0 taken 45697 times.
✓ Branch 1 taken 198960 times.
244657 if (target->b_instr[0].i_loc.lineno < 0) {
9409 45697 target->b_instr[0].i_loc = prev_location;
9410 }
9411 }
9412 }
9413 }
9414 448800 }
9415
9416 /* Perform optimizations on a control flow graph.
9417 The consts object should still be in list form to allow new constants
9418 to be appended.
9419
9420 All transformations keep the code size the same or smaller.
9421 For those that reduce size, the gaps are initially filled with
9422 NOPs. Later those NOPs are removed.
9423 */
9424
9425 static int
9426 448800 optimize_cfg(basicblock *entryblock, PyObject *consts, PyObject *const_cache)
9427 {
9428 assert(PyDict_CheckExact(const_cache));
9429
2/2
✓ Branch 0 taken 1366225 times.
✓ Branch 1 taken 448800 times.
1815025 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9430
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1366225 times.
1366225 if (optimize_basic_block(const_cache, b, consts)) {
9431 return -1;
9432 }
9433 1366225 clean_basic_block(b);
9434 assert(b->b_predecessors == 0);
9435 }
9436
2/2
✓ Branch 0 taken 1366225 times.
✓ Branch 1 taken 448800 times.
1815025 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9437
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1366225 times.
1366225 if (extend_block(b)) {
9438 return -1;
9439 }
9440 }
9441
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 448800 times.
448800 if (mark_reachable(entryblock)) {
9442 return -1;
9443 }
9444 /* Delete unreachable instructions */
9445
2/2
✓ Branch 0 taken 1366225 times.
✓ Branch 1 taken 448800 times.
1815025 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9446
2/2
✓ Branch 0 taken 102752 times.
✓ Branch 1 taken 1263473 times.
1366225 if (b->b_predecessors == 0) {
9447 102752 b->b_iused = 0;
9448 }
9449 }
9450 448800 eliminate_empty_basic_blocks(entryblock);
9451
2/2
✓ Branch 0 taken 1262687 times.
✓ Branch 1 taken 448800 times.
1711487 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9452 1262687 clean_basic_block(b);
9453 }
9454 448800 return 0;
9455 }
9456
9457 // Remove trailing unused constants.
9458 static int
9459 448800 trim_unused_consts(basicblock *entryblock, PyObject *consts)
9460 {
9461 assert(PyList_CheckExact(consts));
9462
9463 // The first constant may be docstring; keep it always.
9464 448800 int max_const_index = 0;
9465
2/2
✓ Branch 0 taken 1262784 times.
✓ Branch 1 taken 448800 times.
1711584 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9466
2/2
✓ Branch 0 taken 14562721 times.
✓ Branch 1 taken 1262784 times.
15825505 for (int i = 0; i < b->b_iused; i++) {
9467
2/2
✓ Branch 0 taken 12369464 times.
✓ Branch 1 taken 2193257 times.
14562721 if ((b->b_instr[i].i_opcode == LOAD_CONST ||
9468
2/2
✓ Branch 0 taken 53716 times.
✓ Branch 1 taken 12315748 times.
12369464 b->b_instr[i].i_opcode == KW_NAMES) &&
9469
2/2
✓ Branch 0 taken 861802 times.
✓ Branch 1 taken 1385171 times.
2246973 b->b_instr[i].i_oparg > max_const_index) {
9470 861802 max_const_index = b->b_instr[i].i_oparg;
9471 }
9472 }
9473 }
9474
2/2
✓ Branch 1 taken 2645 times.
✓ Branch 2 taken 446155 times.
448800 if (max_const_index+1 < PyList_GET_SIZE(consts)) {
9475 //fprintf(stderr, "removing trailing consts: max=%d, size=%d\n",
9476 // max_const_index, (int)PyList_GET_SIZE(consts));
9477
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2645 times.
2645 if (PyList_SetSlice(consts, max_const_index+1,
9478 PyList_GET_SIZE(consts), NULL) < 0) {
9479 return 1;
9480 }
9481 }
9482 448800 return 0;
9483 }
9484
9485 static inline int
9486 946224 is_exit_without_lineno(basicblock *b) {
9487
2/2
✓ Branch 1 taken 638272 times.
✓ Branch 2 taken 307952 times.
946224 if (!basicblock_exits_scope(b)) {
9488 638272 return 0;
9489 }
9490
2/2
✓ Branch 0 taken 386275 times.
✓ Branch 1 taken 69187 times.
455462 for (int i = 0; i < b->b_iused; i++) {
9491
2/2
✓ Branch 0 taken 238765 times.
✓ Branch 1 taken 147510 times.
386275 if (b->b_instr[i].i_loc.lineno >= 0) {
9492 238765 return 0;
9493 }
9494 }
9495 69187 return 1;
9496 }
9497
9498 /* PEP 626 mandates that the f_lineno of a frame is correct
9499 * after a frame terminates. It would be prohibitively expensive
9500 * to continuously update the f_lineno field at runtime,
9501 * so we make sure that all exiting instruction (raises and returns)
9502 * have a valid line number, allowing us to compute f_lineno lazily.
9503 * We can do this by duplicating the exit blocks without line number
9504 * so that none have more than one predecessor. We can then safely
9505 * copy the line number from the sole predecessor block.
9506 */
9507 static int
9508 448800 duplicate_exits_without_lineno(basicblock *entryblock)
9509 {
9510 /* Copy all exit blocks without line number that are targets of a jump.
9511 */
9512
2/2
✓ Branch 0 taken 1277347 times.
✓ Branch 1 taken 448800 times.
1726147 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9513
4/4
✓ Branch 0 taken 1262784 times.
✓ Branch 1 taken 14563 times.
✓ Branch 3 taken 433031 times.
✓ Branch 4 taken 829753 times.
1277347 if (b->b_iused > 0 && is_jump(&b->b_instr[b->b_iused-1])) {
9514 433031 basicblock *target = b->b_instr[b->b_iused-1].i_target;
9515
4/4
✓ Branch 1 taken 50819 times.
✓ Branch 2 taken 382212 times.
✓ Branch 3 taken 14660 times.
✓ Branch 4 taken 36159 times.
433031 if (is_exit_without_lineno(target) && target->b_predecessors > 1) {
9516 14660 basicblock *new_target = copy_basicblock(target);
9517
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14660 times.
14660 if (new_target == NULL) {
9518 return -1;
9519 }
9520 14660 new_target->b_instr[0].i_loc = b->b_instr[b->b_iused-1].i_loc;
9521 14660 b->b_instr[b->b_iused-1].i_target = new_target;
9522 14660 target->b_predecessors--;
9523 14660 new_target->b_predecessors = 1;
9524 14660 new_target->b_next = target->b_next;
9525 14660 target->b_next = new_target;
9526 }
9527 }
9528 }
9529 /* Eliminate empty blocks */
9530
2/2
✓ Branch 0 taken 1262784 times.
✓ Branch 1 taken 448800 times.
1711584 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9531
4/4
✓ Branch 0 taken 828547 times.
✓ Branch 1 taken 448800 times.
✓ Branch 2 taken 14563 times.
✓ Branch 3 taken 813984 times.
1277347 while (b->b_next && b->b_next->b_iused == 0) {
9532 14563 b->b_next = b->b_next->b_next;
9533 }
9534 }
9535 /* Any remaining reachable exit blocks without line number can only be reached by
9536 * fall through, and thus can only have a single predecessor */
9537
2/2
✓ Branch 0 taken 1262784 times.
✓ Branch 1 taken 448800 times.
1711584 for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
9538
4/6
✓ Branch 1 taken 513193 times.
✓ Branch 2 taken 749591 times.
✓ Branch 3 taken 513193 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 513193 times.
✗ Branch 6 not taken.
1262784 if (BB_HAS_FALLTHROUGH(b) && b->b_next && b->b_iused > 0) {
9539
2/2
✓ Branch 1 taken 18368 times.
✓ Branch 2 taken 494825 times.
513193 if (is_exit_without_lineno(b->b_next)) {
9540 assert(b->b_next->b_iused > 0);
9541 18368 b->b_next->b_instr[0].i_loc = b->b_instr[b->b_iused-1].i_loc;
9542 }
9543 }
9544 }
9545 448800 return 0;
9546 }
9547
9548
9549 /* Retained for API compatibility.
9550 * Optimization is now done in optimize_cfg */
9551
9552 PyObject *
9553 PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
9554 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
9555 {
9556 Py_INCREF(code);
9557 return code;
9558 }
9559